簡體   English   中英

如何在 python 中創建一個鍵盤偵聽器,它將返回按下的鍵的字符?

[英]How do I create a keyboard listener in python that will return the character of the pressed key?

例如,當按下“f”鍵時,function 將返回字符“f”,這樣我就可以運行 ord(key) 並獲取按下的鍵的 ascii 值。 我正在嘗試制作一個模擬 Enigma cypher 機器的程序,並希望在 window 處於焦點時直接從鍵盤給出輸入,並將 cypher 輸出打印到控制台上。 (在單獨但相關的注釋中,我無法記住如何讓 print() function 在輸出后省略換行符)。 在我看來,pynput 鍵盤模塊就是這個工具,但我不太清楚如何使用它。 這是我到目前為止的完整代碼。 “on_press”和“on_release”函數,以及“with Listener(...”之后的所有內容都來自互聯網( https://pythonhosted.org/pynput/keyboard.html )老實說,我不知道他們做什么或如何使用它們。

from pynput.keyboard import Key, Listener

wheels = {fst:[19,-1 , 4 , -2, 11,-3 , 12,-4 , 8 ,-5 , 10,-6 , 9 , 0 , 11,-8 , 8 ,-9 , 5 ,-10, 2 ,-10,-5 ,-13,-10,-13],
          med:[2 , 6 , 10, 5 , 0 , 15, 12,-6 , 13, 2 ,-10, 11,-3 ,-7 , 0 , 8 ,-13,-1 ,-5 ,-9 ,-15, 4 ,-3 ,-8 ,-7 ,-1 ],
          slo:[16, 17, 19, 16,-3 ,-2 , 4 , 17, 6 , 0 ,-8 ,-3 , 13,-9 ,-7 ,-10,-16,-6 ,-5 ,-4 , 3 ,-4 ,-2 ,-1 ,-18,-13],
          rfl:[8 , 22, 18, 4 , 1 ,-1 , 19,-4 ,-8 , 5 , 2 , 8 ,-2 , 3 ,-5 , 7 ,-3 , 1 ,-1 ,-8 ,-18, 3 ,-7 ,-22,-3 ,-19]}
dials = [0, 0, 0, 0]

def incr():
  dials[0] = (dials[0] + 1) % 26
  if(dials[0]==0):
    dials[1] = (dials[1] + 1) % 26
  if(dials[1]==0):
    dials[2] = (dials[2] + 1) % 26

def shift(char,level):
  return(chr(ord(char) - 97 + wheels[level][(ord(char) - 97 + dials[level]) % 26]))
  
def on_press(key):
  print('{0} pressed'.format(key))

def on_release(key):
  print('{0} release'.format(key))
  if key == Key.esc:
    return False

def route(char):
  #process the character through each level of scrambling
  incr()

with Listener(on_press=on_press, on_release=on_release) as listener:
  listener.join() 

Listeners 的一般精神是,他們會“聆聽”您的鍵盤以查找您可能鍵入的任何鍵。

在上面的代碼中(取自網站),當您按下某個鍵時會調用on_press function,這與釋放鍵不同。 這個 function 很有用,因為我們可以嘗試存儲按下的信息(鍵)以供以后使用(對於您的 Enigma cypher 機器)。

on_release function 可以被認為是 function,它有助於確定 Listener 是否應該停止聽您的鍵盤。 如果on_release function 返回 False,則監聽器結束。

出於您項目的目的,我對上面的代碼進行了一些粗略的修改,這應該可以幫助您繼續聽鍵盤。

from pynput.keyboard import Key, Listener

wheels = {'fst':[19,-1 , 4 , -2, 11,-3 , 12,-4 , 8 ,-5 , 10,-6 , 9 , 0 , 11,-8 , 8 ,-9 , 5 ,-10, 2 ,-10,-5 ,-13,-10,-13],
          'med':[2 , 6 , 10, 5 , 0 , 15, 12,-6 , 13, 2 ,-10, 11,-3 ,-7 , 0 , 8 ,-13,-1 ,-5 ,-9 ,-15, 4 ,-3 ,-8 ,-7 ,-1 ],
          'slo':[16, 17, 19, 16,-3 ,-2 , 4 , 17, 6 , 0 ,-8 ,-3 , 13,-9 ,-7 ,-10,-16,-6 ,-5 ,-4 , 3 ,-4 ,-2 ,-1 ,-18,-13],
          'rfl':[8 , 22, 18, 4 , 1 ,-1 , 19,-4 ,-8 , 5 , 2 , 8 ,-2 , 3 ,-5 , 7 ,-3 , 1 ,-1 ,-8 ,-18, 3 ,-7 ,-22,-3 ,-19]}
dials = [0, 0, 0, 0]

def incr():
  dials[0] = (dials[0] + 1) % 26
  if(dials[0]==0):
    dials[1] = (dials[1] + 1) % 26
  if(dials[1]==0):
    dials[2] = (dials[2] + 1) % 26

def shift(char,level):
  return(chr(ord(char) - 97 + wheels[level][(ord(char) - 97 + dials[level]) % 26]))

def on_press(key):
  # Use global variable input in order to keep track of current word.
  global userInput
  try:
    userInput += key.char
  except AttributeError:
    #print('special key {0} pressed'.format(key))
    if key == Key.space: 
      userInput += ' '

def on_release(key):
  if key == Key.enter:
    return False
  elif key == Key.esc: 
    return False

def route(char):
  #process the character through each level of scrambling
  incr()


# esc to escape listener.
userInput = ""
with Listener(on_press=on_press, on_release=on_release) as listener:
  listener.join()
print('userInput', userInput)

從此時起,您必須將用戶輸入合並到您機器的 Enigma cypher 方面。 我應該提到我對你的代碼做了一些小的修改。 例如,Listener 應該能夠理解空間。

如果您有任何后續問題,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM