检测键盘按下

2024-09-27 09:30:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个程序,需要能够检测到窗外的键盘按下。我如何才能做到以下几点:

while True:
    if getKey() == w:
        #Do a thing
    if getKey() == a:
        #Do a different thing

Tags: 程序trueif键盘dothingwhiledifferent
1条回答
网友
1楼 · 发布于 2024-09-27 09:30:46

假设“outside of the window”实际上意味着“outside of the console”,您可以尝试以下类似的方法将关键字事件附加到输出文件中。在

import win32api 
import sys
import pythoncom, pyHook

buffer = ''
def OnKeyboardEvent(event):
if event.Ascii == 5: 
  sys.exit() 
if event.Ascii != 0 or 8: 
  f = open ('c:\\output.txt', 'a') 
  keylogs = chr(event.Ascii) 
if event.Ascii == 13: 
  keylogs = keylogs + '\n' 
  f.write(keylogs) 
  f.close() 
while True:
  hm = pyHook.HookManager() 
  hm.KeyDown = OnKeyboardEvent 
  hm.HookKeyboard() 
  pythoncom.PumpMessages()

http://antihackingtutorials.blogspot.de/2012/06/in-this-tutorial-we-will-show-you-how.html

相关问题 更多 >

    热门问题