Python 在 Windows 上发送密钥

发布于 2024-11-26 17:07:09 字数 686 浏览 8 评论 0原文

我正在尝试在 Windows 机器上通过 python 2.7 发送键盘命令。据我所知,SendKeys 模块不适用于 2.7。所以我使用 win32 api 中的 keybd_event 来代替。

我要发送的键是shift+左键来选择一个单词。这是代码:

win32api.keybd_event(win32con.VK_LSHIFT,0,win32con.KEYEVENTF_EXTENDEDKEY | 0,0)
win32api.keybd_event(win32con.VK_LEFT,0,0,0)
win32api.keybd_event(win32con.VK_LSHIFT,0,win32con.KEYEVENTF_EXTENDEDKEY | win32con.KEYEVENTF_KEYUP,0)

这一切都有效。然而,Shift 键保持按下状态!即使在我杀死该进程之后。之后释放 Shift 键的唯一方法是物理按 CapsLock,然后按 Shift + 键,然后关闭 CapsLock。它与这个问题非常相似: http://www.bigresource.com/Tracker/Track -vb-RvVMlrAbZn/

你知道该怎么做吗?

提前致谢!

I'm trying to send keyboard commands through python 2.7 on a windows machine. I understand that the SendKeys module does not work with 2.7. So I use keybd_event from the win32 api instead.

The key that i want to send is shift+left to select a word. This is the code:

win32api.keybd_event(win32con.VK_LSHIFT,0,win32con.KEYEVENTF_EXTENDEDKEY | 0,0)
win32api.keybd_event(win32con.VK_LEFT,0,0,0)
win32api.keybd_event(win32con.VK_LSHIFT,0,win32con.KEYEVENTF_EXTENDEDKEY | win32con.KEYEVENTF_KEYUP,0)

This all works. However, the shift key stays depressed! Even after I kill the process. The only way to release the shift key after is to physically press capslock, then shift + a key, then capslock off. Its very similiar to this problem: http://www.bigresource.com/Tracker/Track-vb-RvVMlrAbZn/

Do you have any idea what to do?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女七分熟 2024-12-03 17:07:09

虽然我找不到让 win32api 与 shift 配合使用的方法,但我正在使用 http: //code.google.com/p/sendkeys-ctypes/ 现在,这似乎适用于 2.7。

While i couldnt find a way to get win32api to work with shift, I'm using SendKeys from http://code.google.com/p/sendkeys-ctypes/ right now, which seems to work with 2.7.

路弥 2024-12-03 17:07:09

Windows API 中的 SendInput 可能会有所帮助。最近的 Raymond Chen 博客文章建议您甚至可以使用它来控制屏幕保护程序的启动/停止。

SendInput from the windows API may be helpful. The recent Raymond Chen blog entry suggests you can even use it to control screensaver start/stop.

夜深人未静 2024-12-03 17:07:09

您可以尝试 pywin32 包,请注意它是旧的并且无人维护。

这是来自 StackOverflow 的示例

You could try the pywin32 package, please note it is old and unmaintained.

Here's an example from StackOverflow

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文