windows下使用python发送控制键
在windows下使用python进行图形类操作时,经常需要用到发送快捷键的情况,如ALT+TAB、crtl + F1、Fn + f1等之类的操作,利用win32com模块,可以很便利的进行该类操作。如下一个示例是打开Outlook程序,获取第一个元素,并将其内容复制到粘贴板的示例:
1import win32com.client
2shell = win32com.client.Dispatch("WScript.Shell")
3shell.Run("outlook")
4shell.AppActivate("Outlook")
5shell.SendKeys("^o", 0)
6shell.SendKeys("^a", 0)
7shell.SendKeys("^c", 0)
部分键映射关系如下:
Key | Argument |
---|---|
BACKSPACE | {BACKSPACE}, {BS}, or {BKSP} |
BREAK | {BREAK} |
CAPS LOCK | {CAPSLOCK} |
DEL or DELETE | {DELETE} or {DEL} |
DOWN ARROW | {DOWN} |
END | {END} |
ENTER | {ENTER} or ~ |
ESC | {ESC} |
HELP | {HELP} |
HOME | {HOME} |
INS or INSERT | {INSERT} or {INS} |
LEFT ARROW | {LEFT} |
NUM LOCK | {NUMLOCK} |
PAGE DOWN | {PGDN} |
PAGE UP | {PGUP} |
PRINT SCREEN | {PRTSC} |
RIGHT ARROW | {RIGHT} |
SCROLL LOCK | {SCROLLLOCK} |
TAB | {TAB} |
UP ARROW | {UP} |
F1 | {F1} |
F2 | {F2} |
F3 | {F3} |
F4 | {F4} |
F5 | {F5} |
F6 | {F6} |
F7 | {F7} |
F8 | {F8} |
F9 | {F9} |
F10 | {F10} |
F11 | {F11} |
F12 | {F12} |
F13 | {F13} |
F14 | {F14} |
F15 | {F15} |
F16 | {F16} |
几个特殊键键映射关系如下:
Key | Special Character |
---|---|
SHIFT | + |
CTRL | ^ |
ALT | % |
ENTER | ~ |
通过以上映射关系表,如实现ctrl + F5 强制刷新浏览器页面就比较容易实现,shell.SendKeys(“^{F5}”, 0) 即可。
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/windows-python-sendkeys/5182.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.