Skip to content

Instantly share code, notes, and snippets.

@timrprobocom
Created March 29, 2017 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timrprobocom/c00c0e42c4d4ed3618ed01e3dfc9657b to your computer and use it in GitHub Desktop.
Save timrprobocom/c00c0e42c4d4ed3618ed01e3dfc9657b to your computer and use it in GitHub Desktop.
Example showing key chord handling with wxPython
import wx
class Gui(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="test", size=(800, 400))
self.txt = wx.TextCtrl( self, -1, "Hello", style=wx.TE_MULTILINE )
self.txt.Bind( wx.EVT_KEY_DOWN, self.onKeyDown )
self.txt.Bind( wx.EVT_KEY_UP, self.onKeyUp )
self.keylist = []
self.timer = wx.Timer(self)
self.Bind( wx.EVT_TIMER, self.onTimer, self.timer )
self.handled = False
def onKeyDown( self, evt ):
print( "Key down", evt.GetKeyCode() )
if not self.keylist:
self.handled = False
self.timer.Start( 100 )
self.keylist.append( evt.GetKeyCode() )
def onKeyUp( self, evt ):
print( "Key up", evt.GetKeyCode() )
if not self.handled:
self.handleChord()
self.keylist.remove( evt.GetKeyCode() )
def onTimer( self, evt ):
print( "Timer" )
if not self.handled:
self.handleChord()
def handleChord( self ):
self.timer.Stop()
msg = ' '.join(chr(k) for k in self.keylist)
self.txt.AppendText( "Chord: " + msg + "\r\n" )
self.handled = True
if __name__ == '__main__':
app = wx.App(redirect=False)
top = Gui()
top.Show()
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment