Created
March 29, 2017 17:25
-
-
Save timrprobocom/c00c0e42c4d4ed3618ed01e3dfc9657b to your computer and use it in GitHub Desktop.
Example showing key chord handling with wxPython
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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