Typing Keybord Key events



Typing event to track keys in Tkinter

Without using the Mouse or by clicking the buttons, we can simulate the Click events of the buttons. Here we have one line of Keys of our Keyboard to show the key press and key release events. This can be extended to match all the keys of the keypad.

Tkinter Managing typing event by using key and KeyRelease events by winfo_children() list

Using Key press event

Here if any key is pressed then we will trigger the callback function my_callback() to execute the code.
my_w.bind('<Key>',my_callback) # Any Key  is pressed
Here inside the function first we will check which key is pressed by using event.char. The same we will use to update the Label l1 to print the char associated with the Key.
l1.config(text=event.char) # Update the text on Label 
We can get all the widget classes of the application by using winfo_children(). We can loop through the list of widgets and check for matching button class.

We can read the buttons text option to find out the string written over the button and if it matches with the key pressed then the button's relief attribute will be updated to sunken by displaying the button press appearance.
def my_callback(event): # When any key is pressed
     l1.config(text=event.char) # Update the text on Label 
     for widget in my_w.winfo_children():  # Collect all classes of the widgets 
        if isinstance(widget, tk.Button):  # If it belongs to button class 
            if widget['text']==event.char or widget['text'].lower()==event.char:
               widget['relief']='sunken'   # Update relief option to pressed 

Using Key Release event

The above code will create the button press appearance, however to create the button release appearance we have to update the relief attribute to raised. This we will do irrespective of which button is released.
To trigger the event
my_w.bind("<KeyRelease>",my_release)
Inside our function my_release() we will have update the relief attribute of all buttons to raised. Here also we will list all widgets of the application by using winfo_children().
def my_release(event):
     for widget in my_w.winfo_children():   # all widget classes of the application 
          if isinstance(widget, tk.Button): # if belongs to button class 
               widget['relief']='raised'  # Update the relief option to raised
Full code is here
import tkinter as tk # Python 3
my_w = tk.Tk()
my_w.geometry("475x200") 

def my_callback(event): # When any key is pressed
     l1.config(text=event.char) # Update the text on Label 
     for widget in my_w.winfo_children():  # Collect all classes of the widgets 
        if isinstance(widget, tk.Button):  # If it belongs to button class 
            if widget['text']==event.char or widget['text'].lower()==event.char:
               widget['relief']='sunken'   # Update relief option to pressed 
         
def my_release(event):
     for widget in my_w.winfo_children():   # all widget classes of the application 
          if isinstance(widget, tk.Button): # if belongs to button class 
               widget['relief']='raised'  # Update the relief option to raised

ba=tk.Button(my_w,text='A',font=28,width=2)
ba.grid(row=0,column=0,padx=10,pady=20)

bs=tk.Button(my_w,text='S',font=28,width=2)
bs.grid(row=0,column=1,padx=10,pady=10)

bd=tk.Button(my_w,text='D',font=28,width=2)
bd.grid(row=0,column=2,padx=10,pady=10)

bf=tk.Button(my_w,text='F',font=28,width=2)
bf.grid(row=0,column=3,padx=10,pady=10)

bg=tk.Button(my_w,text='G',font=28,width=2)
bg.grid(row=0,column=4,padx=10,pady=10)

bh=tk.Button(my_w,text='H',font=28,width=2)
bh.grid(row=0,column=5,padx=10,pady=10)

bj=tk.Button(my_w,text='J',font=28,width=2)
bj.grid(row=0,column=6,padx=10,pady=10)

bk=tk.Button(my_w,text='K',font=28,width=2)
bk.grid(row=0,column=7,padx=10,pady=10)

bl=tk.Button(my_w,text='L',font=28,width=2)
bl.grid(row=0,column=8,padx=10,pady=10)

l1=tk.Label(my_w,text='#',bg='yellow',width=2,font=('Times',26,'normal'))
l1.grid(row=1,column=0,padx=2,pady=10,columnspan=3)

my_w.bind('<Key>',my_callback) # Any Key  is pressed
#my_w.bind('<s>',my_callback) # Key s is pressed
my_w.bind("<KeyRelease>",my_release)
my_w.mainloop()

Integrating Click events with the Key press

Typing event with invoke()

We can use invoke() to trigger click events without clicking the buttons.

invoke(): Button Press event without Clicking

Here to one of our button ( say K ) we will add one onClick command. Once the button K is pressed, we will print one welcome message at our console.
Here is the code for the Button K.
bk=tk.Button(my_w,text='K',font=28,width=2,command=lambda:my_click())
bk.grid(row=0,column=7,padx=10,pady=10)
We will add one function my_click()
def my_click():
    print('Welcome to plus2net')
    l2.config(text='plus2net.com')
Inside our function my_callback() we will add invoke()
def my_callback(event): # When any key is pressed
     l1.config(text=event.char) # Update the text on Label 
     for widget in my_w.winfo_children():  # Collect all classes of the widgets 
        if isinstance(widget, tk.Button):  # If it belongs to button class 
            if widget['text']==event.char or widget['text'].lower()==event.char:
               widget['relief']='sunken'   # Update relief option to pressed 
               widget.invoke()


Tkinter Key event to trigger button command click event by using invoke()
Full code is here
import tkinter as tk # Python 3
my_w = tk.Tk()
my_w.geometry("475x200") 

def my_callback(event): # When any key is pressed
     l1.config(text=event.char) # Update the text on Label 
     for widget in my_w.winfo_children():  # Collect all classes of the widgets 
        if isinstance(widget, tk.Button):  # If it belongs to button class 
            if widget['text']==event.char or widget['text'].lower()==event.char:
               widget['relief']='sunken'   # Update relief option to pressed 
               widget.invoke()
         
def my_release(event):
     for widget in my_w.winfo_children():   # all widget classes of the application 
          if isinstance(widget, tk.Button): # if belongs to button class 
               widget['relief']='raised'  # Update the relief option to raised
def my_click():
    print('Welcome to plus2net')
    l2.config(text='plus2net.com')
    
ba=tk.Button(my_w,text='A',font=28,width=2)
ba.grid(row=0,column=0,padx=10,pady=20)

bs=tk.Button(my_w,text='S',font=28,width=2)
bs.grid(row=0,column=1,padx=10,pady=10)

bd=tk.Button(my_w,text='D',font=28,width=2)
bd.grid(row=0,column=2,padx=10,pady=10)

bf=tk.Button(my_w,text='F',font=28,width=2)
bf.grid(row=0,column=3,padx=10,pady=10)

bg=tk.Button(my_w,text='G',font=28,width=2)
bg.grid(row=0,column=4,padx=10,pady=10)

bh=tk.Button(my_w,text='H',font=28,width=2)
bh.grid(row=0,column=5,padx=10,pady=10)

bj=tk.Button(my_w,text='J',font=28,width=2)
bj.grid(row=0,column=6,padx=10,pady=10)

bk=tk.Button(my_w,text='K',font=28,width=2,command=lambda:my_click())
bk.grid(row=0,column=7,padx=10,pady=10)

bl=tk.Button(my_w,text='L',font=28,width=2)
bl.grid(row=0,column=8,padx=10,pady=10)

l1=tk.Label(my_w,text='#',bg='yellow',width=2,font=('Times',26,'normal'))
l1.grid(row=1,column=0,padx=2,pady=10,columnspan=3)

l2=tk.Label(my_w,text='#',bg='lightyellow',font=('Times',26,'normal'))
l2.grid(row=2,column=0,padx=2,pady=10,columnspan=6)

my_w.bind('<Key>',my_callback) # Any Key  is pressed
#my_w.bind('<s>',my_callback) # Key s is pressed
my_w.bind("<KeyRelease>",my_release)
my_w.mainloop()
Moving elements using arrow keys. Tkinter Canvas Animation using Rectangles & Circles
Sin & cos curves in Canvas More Projects using Tkinter
Events
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer