利用Python win32api 模擬鍵盤輸入,打開瀏覽器,實現網頁全屏,並移到第二屏幕


 1 import webbrowser
 2 import win32api
 3 import win32gui
 4 import time
 5 
 6 #Define the url address
 7 url_baidu_home = "http://www.baidu.com/"
 8 url_baiduNews_tags = "http://news.baidu.com/"
 9 odw_baiduMusic_tags = "http://play.baidu.com/"
10 odw_baiduXueshu_tags = "http://xueshu.baidu.com/"
11   
12 def maxWindow():
13     win32api.keybd_event(122,0,0,0) #F11
14     win32api.keybd_event(122,0,win32con.KEYEVENT_KEYUP,0) #Realize the F11 button
15 
16 def switchTab():
17     win32api.keybd_event(17,0,0,0) #Ctrl
18     win32api.keybd_event(9,0,0,0) #Tab
19     win32api.keybd_event(17,0,KEYEVENT_KEYUP,0) #Realize the Ctrl button
20     win32api.keybd_event(19,0,KEYEVENT_KEYUP,0) #Realize the Tab button
21 
22 hwnd = win32gui.GetForegroundWindow() #Get the 句柄
23 win32gui.MoveWindow(hwnd,1980,0,1980,1080,False) #Move Window to the second screen
24 maxWindow()
25 count = 0
26 while (count<1000):
27     switchTab() 
28     time.sleep(3)
29     count=count+1

附個鍵位碼表:
字母和數字鍵     數字小鍵盤的鍵       功能鍵         其它鍵 
      鍵   鍵碼   鍵   鍵碼     鍵   鍵碼    鍵      鍵碼 
      A   65       0   96        F1   112     Backspace    8 
      B   66       1   97        F2   113     Tab       9 
      C   67       2   98        F3   114     Clear      12 
      D   68       3   99        F4   115     Enter      13 
      E   69       4   100       F5   116     Shift      16 
      F   70       5   101       F6   117     Control     17 
      G   71       6   102       F7   118      Alt       18 
      H   72       7   103       F8   119     Caps Lock    20 
      I   73       8   104       F9   120     Esc       27 
      J   74       9   105       F10  121     Spacebar    32 
      K   75       *   106       F11  122     Page Up     33 
      L   76       +   107       F12  123     Page Down    34 
      M   77       Enter 108       --   --      End       35 
      N   78       -   109       --   --       Home      36 
      O   79       .   110       --   --      Left Arrow   37 
      P   80       /   111       --   --      Up Arrow    38 
      Q   81       --   --       --   --      Right Arrow   39 
      R   82       --   --       --   --      Down Arrow    40 
      S   83       --   --       --   --      Insert      45 
      T   84       --   --       --   --      Delete      46 
      U   85       --   --       --   --      Help       47 
      V   86       --   --       --   --      Num Lock     144 
      W   87          
      X   88      
      Y   89      
      Z   90      
      0   48      
      1   49      
      2   50       
      3   51       
      4   52       
      5   53       
      6   54       
      7   55       
      8   56       
      9   57

今兒,深深地感受到了python的強大之處,換作java不知要寫多少行代碼呢。。。

主要是模塊的調用;

推薦一個網址,有很多關於win32gui與win32api的小demo;不得不說,這兩個模塊功能太太太太...

http://nullege.com/codes/search/win32gui

http://nullege.com/codes/search?cq=win32api

關於win32gui.MoveWindow()內的參數解釋在下面的鏈接里。

https://msdn.microsoft.com/en-us/library/windows/desktop/ms633534%28v=vs.85%29.aspx

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM