首页 > 解决方案 > 有没有办法在 python 中发送/按下 NUMPAD 键?

问题描述

到目前为止,我已经尝试 pyautogui 发送它们,但它不起作用。我希望是因为在源代码中它显示了这一点:

'num multiply': '*',
'num divide': '/',
'num add': '+',
'num plus': '+',
'num minus': '-',
'num sub': '-',
'num enter': 'enter',
'num 0': '0',
'num 1': '1',
'num 2': '2',
'num 3': '3',
'num 4': '4',
'num 5': '5',
'num 6': '6',
'num 7': '7',
'num 8': '8',
'num 9': '9',

但这并没有做任何你可能期望的事情。它会做常规数字,而不是数字键盘特定的数字。

pyautogui.press("num 3")

例如,根本不起作用,并且

pyautogui.press("3")

只发一个 3

我也尝试过 win32api 的 SendKeys,但是根本没有列出 numkeys,并且通过那里的源代码,看起来它们被包含在内(https://docs.microsoft.com/en-us/ windows/desktop/inputdev/virtual-key-codes)但在 Python 实现中不受支持......所以这不起作用:

>>> import win32api
>>> import win32com.client
>>> shell = win32com.client.Dispatch("WScript.Shell")
>>> shell.SendKeys("{NUMPAD7}")

返回

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024809), None)

我需要能够使用数字键盘键,因为我有一个使用模拟鼠标的设置,我需要通过这些键以编程方式控制它。我已经通过有效的组合键(并且可以使用 SendKeys 发送)禁用和重新启用仿真(我正在使用 NeatMouse)找到了一种解决方法,因为您很少同时键入和使用鼠标,但是我仍然很好奇我想做的事情是否可以直接直接执行。

重申一下,我想通过 python 直接按 NUMPAD 键。

谢谢!

标签: pythoninputkeyboardmouseemulation

解决方案


根据官方文档:PyAutoGUI - Keyboard Control Functions ,您的代码不起作用的唯一问题似乎是“num”和“3”之间的空格。如果文档正确,您应该只更改:

pyautogui.press("num 3")

至:

pyautogui.press("num3")

推荐阅读