3

For a project I am making a program controlled completely by the number pad. A lot relies on buttons on the num-pad being able to be detected as being held down or not; I would set a Boolean to true when it is pressed and false when it is released. With these constraints I came across a problem: The Num-Lock key toggles between up and down each time it is pressed, and nothing upon release. With the same system I was using for the other keys, pressing the key once would turn it true and pressing it again would make it false again rather than pressing it making it true and RELEASING it making it false again.

Basically, how can I get the numpad Num-Lock key to trigger something upon both press and release instead of toggling with each press?

Intended OS: Windows and Linux (Raspberry Pi)
Python 3.x

The was I get the inputs is the same as the Python games on raspberry pi, using event.type and event.key etc.

user3666197
  • 1
  • 6
  • 50
  • 92

1 Answers1

1

Event handlers already integrated inside Tkinter may help you a lot:

An event has following attributes:

#       <event>.char        on-{ <KeyPress> | <KeyRelease> }
#              .keysym      on-{ <KeyPress> | <KeyRelease> }
#              .keysym_num  on-{ <KeyPress> | <KeyRelease> }
#              .height      on-{ <Configure> }
#              .width       on-{ <Configure> }
#              .num         on-{ <Mouse-1>  | <Mouse-2> | ... } ? 4,5 == <MouseWheel>
#              .serial      <-- system-assigned Integer
#              .time        <-- system-assigned Integer ( .inc each msec )
#              .widget      <-- system-assigned <widget>-instance
#              .x           <-- system-assigned <Event>-in-<widget>-mouse-location.x
#              .y           <-- system-assigned <Event>-in-<widget>-mouse-location.y
#              .x_root      <-- system-assigned <Event>-on-<Screen>-mouse-location.x
#              .y_root      <-- system-assigned <Event>-on-<Screen>-mouse-location.y

This mock-up demo can show you the mechanics:

    #---------------------------------------------------------------# EVENT HANDLER DEMO:
    #       from Tkinter import *                                   # import Tkinter as tk
            root = Tk()                                             # root = tk.Tk()
            root.lift()    
            #-------------------------------------------------------                                                    
            >>> def aMouseEnterHANDLER( anEvent ):
            ...      print "<Enter>-event: ", anEvent.serial, anEvent.time, anEvent.x, anEvent.y, str( anEvent.widget )
            ...      print 
            ...
            >>> def KeyHANDLER( anEvent ):
            ...      print "<Key>-event: ", anEvent.serial, anEvent.time, anEvent.char, anEvent.keysym, anEvent.keysym_num, str( anEvent.widget )
            ...
            >>> root.bind( "<Enter>", aMouseEnterHANDLER )
            '23852056EnterHandler'
            >>> root.bind( "<KeyPress>", KeyHANDLER )               # root-widget level .bind()
            '23852576KeyHandler'

            >>> root.mainloop()                                     # <-- after entering this, <KeyPress> started to work ----------------------

            <Key>-event:  904 590059546 s s 115 .
            <Enter>-event:  911 590065062 1 5 .
            <Enter>-event:  911 590065062 1 5 .12965248
            <Enter>-event:  915 590067703 4 1 .
            <Enter>-event:  915 590067703 4 1 .12965248
            <Key>-event:  918 590069062 s s 115 .
            <Key>-event:  920 590069906 s s 115 .
            <Key>-event:  922 590070390  Caps_Lock 65509 .
            <Key>-event:  924 590071687  Shift_L 65505 .            
            <Key>-event:  925 590072187  Shift_L 65505 .
            <Key>-event:  926 590072218  Shift_L 65505 .
            <Key>-event:  927 590072250  Shift_L 65505 .
            <Key>-event:  928 590072281  Shift_L 65505 .
            <Key>-event:  929 590072328  Shift_L 65505 .
            <Key>-event:  930 590072359  Shift_L 65505 .
            <Key>-event:  931 590072390  Shift_L 65505 .
            <Key>-event:  932 590072421  Shift_L 65505 .
            <Key>-event:  934 590072890 A A 65 .
            <Key>-event:  936 590073390 A A 65 .
            <Key>-event:  938 590073531 A A 65 .
            <Key>-event:  940 590073718 A A 65 .
            <Key>-event:  942 590074093  Shift_L 65505 .
            <Key>-event:  944 590074625  Shift_L 65505 .
            <Key>-event:  946 590074843  Shift_L 65505 .
            <Key>-event:  948 590075031  Shift_L 65505 .
            <Key>-event:  950 590075656  Shift_L 65505 .
            <Key>-event:  952 590076031  Caps_Lock 65509 .
            <Enter>-event:  956 590109296 0 1 .
            <Enter>-event:  956 590109296 0 1 .12965248
            <Enter>-event:  959 590109734 6 5 .12965248
            <Enter>-event:  962 590110078 9 9 .
            <Enter>-event:  962 590110078 9 9 .12965248
            <Enter>-event:  965 590110328 9 10 .12965248
            <Enter>-event:  968 590111781 40 19 .
            <Key>-event:  968 590113875  Up 65362 .
            <Key>-event:  970 590114781  Left 65361 .
            <Key>-event:  972 590116718 + plus 43 .
            <Key>-event:  974 590117656 - minus 45 .
            Return 65293 .976 590118343
            <Key>-event:  978 590120359 d d 100 .
            <Key>-event:  980 590123312     Tab 65289 .
            <Key>-event:  988 590124140     Tab 65289 .12965248
            <Key>-event:  988 590124140     Tab 65289 .12965248
            <Key>-event:  990 590125578     Tab 65289 .12965248
            <Key>-event:  990 590125578     Tab 65289 .12965248
user3666197
  • 1
  • 6
  • 50
  • 92