Keyboard Event

Posted 2017.09.07 02:11

Very simple code for getting a keyboard event in Python


from pynput import keyboard
import time

def on_press(key):
    try: k = key.char # single-char keys
    except: k = key.name # other keys
    if key == keyboard.Key.esc: return False # stop listener
    if k in ['1', '2', 'left', 'right']: # keys interested
        # self.keys.append(k) # store it in global-like variable
        print('Key pressed: ' + k)
        # return False # remove this if want more keys

lis = keyboard.Listener(on_press=on_press)
lis.start() # start to listen on a separate thread
# lis.join() # no this if main thread is polling self.keys

while 1:
    time.sleep(1)
    print ("TIC")



신고

'Enginius > Python&TensorFlow' 카테고리의 다른 글

PCA in Python using numpy  (0) 2017.10.18
python plot  (0) 2017.09.26
Keyboard Event  (0) 2017.09.07
Simple Handshaking between Matlab and TensorFlow  (0) 2017.04.17
TensorFlow-trained network in MATLAB  (0) 2017.01.10
도커와 텐서플로우 설치하기  (0) 2016.11.30
« PREV : 1 : ··· : 4 : 5 : 6 : 7 : 8 : 9 : 10 : 11 : 12 : ··· : 581 : NEXT »