利用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

原文地址:https://www.cnblogs.com/Annguowenhua/p/5435562.html