본문 바로가기

Enginius/Python&TensorFlow

Keyboard Event

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
SUPER GPU MACHINE 만들기  (0) 2017.08.11
Simple Handshaking between Matlab and TensorFlow  (0) 2017.04.17
TensorFlow-trained network in MATLAB  (0) 2017.01.10