Best Python code snippet using pyatom_python
keyboard_events.py
Source:keyboard_events.py  
1#!/usr/bin/env python32# -*- coding: utf-8 -*-3# Copyright (c) 2015 Jérémie DECOCK (http://www.jdhp.org)4# Permission is hereby granted, free of charge, to any person obtaining a copy5# of this software and associated documentation files (the "Software"), to deal6# in the Software without restriction, including without limitation the rights7# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell8# copies of the Software, and to permit persons to whom the Software is9# furnished to do so, subject to the following conditions:10# The above copyright notice and this permission notice shall be included in11# all copies or substantial portions of the Software.12 13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,18# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN19# THE SOFTWARE.20# See: https://github.com/jeremiedecock/pyarm/blob/master/pyarm/gui/tkinter_gui.py21#      http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/key-names.html22#      http://www.tcl.tk/man/tcl8.4/TkCmd/keysyms.htm23import tkinter as tk24root = tk.Tk()25label = tk.Label(root, text="Press some keys", width=50, height=10)26label.pack()27# SETUP KEYBOARD EVENT CALLBACKS28def keypress_callback(event):29    if event.keysym == "Up":30        print("keypress: <Up>")31    elif event.keysym == "Down":32        print("keypress: <Down>")33    elif event.keysym == "Left":34        print("keypress: <Left>")35    elif event.keysym == "Right":36        print("keypress: <Right>")37    elif event.keysym == "Return":38        print("keypress: <Return>")39    elif event.keysym == "Escape":40        print("keypress: <Escape>")41    elif event.keysym == "space":42        print("keypress: <space>")43    elif event.keysym == "Control_R":44        print("keypress: <Control_R>")45    elif event.keysym == "Control_L":46        print("keypress: <Control_L>")47    elif event.keysym == "Shift_R":48        print("keypress: <Shift_R>")49    elif event.keysym == "Shift_L":50        print("keypress: <Shift_L>")51    elif event.keysym == "Tab":52        print("keypress: <Tab>")53    elif event.keysym == "Super_R":54        print("keypress: <Super_R>")55    elif event.keysym == "Super_L":56        print("keypress: <Super_L>")57    elif event.keysym == "BackSpace":58        print("keypress: <BackSpace>")59    elif event.keysym == "Prior":    # PgUp60        print("keypress: <Prior>")61    elif event.keysym == "Next":     # PgDown62        print("keypress: <Next>")63    elif event.char == "a":64        print("keypress: <a>")65    elif event.char == "b":66        print("keypress: <b>")67    elif event.char == "c":68        print("keypress: <c>")69    elif event.char == "d":70        print("keypress: <d>")71    elif event.char == "A":72        print("keypress: <A>")73    elif event.char == "B":74        print("keypress: <B>")75    elif event.char == "C":76        print("keypress: <C>")77    elif event.char == "D":78        print("keypress: <D>")79    elif event.char == "1":80        print("keypress: <1>")81    elif event.char == "2":82        print("keypress: <2>")83    elif event.char == "3":84        print("keypress: <3>")85    else:86        print("keypress:", event.char, event.keysym)87def keyrelease_callback(event):88    if event.keysym == "Up":89        print("keyrelease: <Up>")90    elif event.keysym == "Down":91        print("keyrelease: <Down>")92    elif event.keysym == "Left":93        print("keyrelease: <Left>")94    elif event.keysym == "Right":95        print("keyrelease: <Right>")96    elif event.keysym == "Return":97        print("keyrelease: <Return>")98    elif event.keysym == "Escape":99        print("keyrelease: <Escape>")100    elif event.keysym == "space":101        print("keyrelease: <space>")102    elif event.keysym == "Control_R":103        print("keyrelease: <Control_R>")104    elif event.keysym == "Control_L":105        print("keyrelease: <Control_L>")106    elif event.keysym == "Shift_R":107        print("keyrelease: <Shift_R>")108    elif event.keysym == "Shift_L":109        print("keyrelease: <Shift_L>")110    elif event.keysym == "Tab":111        print("keyrelease: <Tab>")112    elif event.keysym == "Super_R":113        print("keyrelease: <Super_R>")114    elif event.keysym == "Super_L":115        print("keyrelease: <Super_L>")116    elif event.keysym == "BackSpace":117        print("keyrelease: <BackSpace>")118    elif event.keysym == "Prior":    # PgUp119        print("keyrelease: <Prior>")120    elif event.keysym == "Next":     # PgDown121        print("keyrelease: <Next>")122    elif event.char == "a":123        print("keyrelease: <a>")124    elif event.char == "b":125        print("keyrelease: <b>")126    elif event.char == "c":127        print("keyrelease: <c>")128    elif event.char == "d":129        print("keyrelease: <d>")130    elif event.char == "A":131        print("keyrelease: <A>")132    elif event.char == "B":133        print("keyrelease: <B>")134    elif event.char == "C":135        print("keyrelease: <C>")136    elif event.char == "D":137        print("keyrelease: <D>")138    elif event.char == "1":139        print("keyrelease: <1>")140    elif event.char == "2":141        print("keyrelease: <2>")142    elif event.char == "3":143        print("keyrelease: <3>")144    else:145        print("keyrelease:", event.char, event.keysym)146root.bind("<KeyPress>", keypress_callback)147root.bind("<KeyRelease>", keyrelease_callback)...command.py
Source:command.py  
...33		if not self.__hold:34			print("press", event.char)35			self.__press = True36			self._keypress(event)37	def keyrelease(self, event):38		def f():39			if time.time() - self.__t_press > 0.1:40				print("release", event.char)41				self.__hold = False42				self._keyrelease(event)43			else:44				self.__hold = True45				self._keyhold(event)46		threading.Timer(0.1, f).start()47		48states = {}49states['z'] = 050states['q'] = 051states['s'] = 052states['d'] = 053def send():54	bot.send(".pwm %s %s %s %s" % (states['z'], states['q'], states['s'], states['d']))55def keypress(event):56	global states57	if event.keysym == 'Escape':58		root.destroy()59	x = event.char60	if x=='z':61		states['z'] = 162	elif x=='q':63		states['q'] = 164	elif x=='d':65		states['d'] = 166	elif x=='s':67		states['s'] = 168	else:69		print(x)70	send()71def keyrelease(event):72	global states73	x = event.char74	if x=='z':75		states['z'] = 076	elif x=='q':77		states['q'] = 078	elif x=='d':79		states['d'] = 080	elif x=='s':81		states['s'] = 082	else:83		print(x)84	send()85	...tkinterUserInputExample.py
Source:tkinterUserInputExample.py  
1import tkinter as tk23def keyPress(event):4    print(event)5    if event.keysym == 'Return':6        root.destroy()7        print('Stopping test')89def keyRelease(event):10    print(event)1112def getUserInput():13    root = tk.Tk()14    frame = tk.Frame(root, width= 1, height= 1)15    frame.bind("<KeyPress>", keyPress)16    frame.bind("<KeyRelease>", keyRelease)17    frame.pack()18    frame.focus_set()19    root.mainloop()2021if __name__ == "__main__":22    root = tk.Tk()23    frame = tk.Frame(root, width= 1, height= 1)24    frame.bind("<KeyPress>", keyPress)25    frame.bind("<KeyRelease>", keyRelease)26    frame.pack()27    frame.focus_set()
...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
