From 9d3abce44566540bed2df905105fba281c74a7ce Mon Sep 17 00:00:00 2001 From: James Teh Date: Wed, 8 Dec 2010 16:40:58 +1000 Subject: [PATCH] In global gesture maps, allow gestures to emulate key presses by specifying the target gesture identifier (i.e. kb:keyName) as the script name. This will be used to allow keys on braille displays to emulate key presses on the system keyboard. This is done by generating a fake script. --- source/inputCore.py | 7 +++++-- source/scriptHandler.py | 28 +++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/source/inputCore.py b/source/inputCore.py index 0b9d6da18..f2fc46b71 100644 --- a/source/inputCore.py +++ b/source/inputCore.py @@ -281,8 +281,11 @@ class InputManager(baseObject.AutoPropertyObject): runScript = False if script: scriptName = scriptHandler.getScriptName(script) - scriptLocation=scriptHandler.getScriptLocation(script) - log.info("Input help: gesture %s, bound to script %s on %s"%(gesture.identifiers[0],scriptName,scriptLocation)) + scriptLocation = scriptHandler.getScriptLocation(script) + logMsg = "Input help: gesture %s, bound to script %s" % (gesture.identifiers[0], scriptName) + if scriptLocation: + logMsg += " on %s" % scriptLocation + log.info(logMsg) if scriptName == "toggleInputHelp": runScript = True else: diff --git a/source/scriptHandler.py b/source/scriptHandler.py index 3a83f92ac..07e213cd9 100644 --- a/source/scriptHandler.py +++ b/source/scriptHandler.py @@ -21,6 +21,15 @@ _lastScriptRef=None #Holds a weakref to the last script that was executed _lastScriptCount=0 #The amount of times the last script was repeated _isScriptRunning=False +def _makeKbEmulateScript(scriptName): + import keyboardHandler + keyName = scriptName[3:] + emuGesture = keyboardHandler.KeyboardInputGesture.fromName(keyName) + func = lambda gesture: inputCore.manager.emulateGesture(emuGesture) + func.__name__ = "script_%s" % scriptName + func.__doc__ = _("Emulates pressing %s on the system keyboard") % keyName + return func + def _getObjScript(obj, gesture, globalMapScripts): # Search the scripts from the global gesture maps. for cls, scriptName in globalMapScripts: @@ -28,10 +37,14 @@ def _getObjScript(obj, gesture, globalMapScripts): if scriptName is None: # The global map specified that no script should execute for this gesture and object. return None + if scriptName.startswith("kb:"): + # Emulate a key press. + return _makeKbEmulateScript(scriptName) try: return getattr(obj, "script_%s" % scriptName) except AttributeError: pass + # Search the object itself for in-built bindings. return obj.getScript(gesture) @@ -93,12 +106,16 @@ def getScriptName(script): return script.__name__[7:] def getScriptLocation(script): + try: + instance = script.__self__ + except AttributeError: + # Not an instance method, so this must be a fake script. + return None name=script.__name__ - for cls in script.__self__.__class__.__mro__: + for cls in instance.__class__.__mro__: if name in cls.__dict__: return "%s.%s"%(cls.__module__,cls.__name__) - def _queueScriptCallback(script,gesture): global _numScriptsQueued _numScriptsQueued-=1 @@ -121,13 +138,14 @@ def executeScript(script,gesture): global _lastScriptTime, _lastScriptCount, _lastScriptRef, _isScriptRunning lastScriptRef=_lastScriptRef() if _lastScriptRef else None #We don't allow the same script to be executed from with in itself, but we still should pass the key through - if _isScriptRunning and lastScriptRef==script.im_func: + scriptFunc=getattr(script,"__func__",script) + if _isScriptRunning and lastScriptRef==scriptFunc: return gesture.send() _isScriptRunning=True try: scriptTime=time.time() - scriptRef=weakref.ref(script.im_func) - if (scriptTime-_lastScriptTime)<=0.5 and script.im_func==lastScriptRef: + scriptRef=weakref.ref(scriptFunc) + if (scriptTime-_lastScriptTime)<=0.5 and scriptFunc==lastScriptRef: _lastScriptCount+=1 else: _lastScriptCount=0 -- 2.11.0