From 96a258dd9b135e7ba3787a4f8fd77254942a8903 Mon Sep 17 00:00:00 2001 From: Ibrahim Hamadeh Date: Mon, 20 Sep 2021 05:45:00 +0300 Subject: [PATCH] Add feature to move mouse by object --- .gitignore | 1 + addon/globalPlugins/goldenCursor.py | 84 +++++++++++++++++++++++++++++ readme.md | 1 + 3 files changed, 86 insertions(+) diff --git a/.gitignore b/.gitignore index 76a14ed..1eb4fea 100755 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ addon/doc/en/ *.pyc *.nvda-addon .sconsign.dblite +*.exe diff --git a/addon/globalPlugins/goldenCursor.py b/addon/globalPlugins/goldenCursor.py index 8cee79f..9d880fd 100755 --- a/addon/globalPlugins/goldenCursor.py +++ b/addon/globalPlugins/goldenCursor.py @@ -425,6 +425,13 @@ def script_toggleMouseArrows(self, gesture): self.bindGesture("kb:leftArrow", "moveMouseLeft") self.bindGesture("kb:downArrow", "moveMouseDown") self.bindGesture("kb:upArrow", "moveMouseUp") + _dict={ + "kb:control+rightArrow": "moveByObjectRight", + "kb:control+leftArrow": "moveByObjectLeft", + "kb:control+downArrow": "moveByObjectDown", + "kb:control+upArrow": "moveByObjectUp" + } + self.bindGestures(_dict) # Translators: presented when toggling mouse arrows feature. ui.message(_("Mouse arrows on")) else: @@ -524,6 +531,83 @@ def moveMouse(self, direction): if config.conf["goldenCursor"]["reportNewMouseCoordinates"]: ui.message(str(x if direction in (GCMouseRight, GCMouseLeft) else y)) + @scriptHandler.script( + # Translators: Input help message for a Golden Cursor command. + description=_("Moves the Mouse pointer right to the adjacent object "), + gesture="kb:nvda+control+windows+rightArrow" + ) + def script_moveByObjectRight(self, gesture): + self.moveMouseByObject(GCMouseRight) + + + @scriptHandler.script( + # Translators: Input help message for a Golden Cursor command. + description=_("Moves the Mouse pointer left to the adjacent object "), + gesture="kb:nvda+control+windows+leftArrow" + ) + def script_moveByObjectLeft(self,gesture): + self.moveMouseByObject(GCMouseLeft) + + @scriptHandler.script( + # Translators: Input help message for a Golden Cursor command. + description=_("Moves the Mouse pointer down to the adjacent object"), + gesture="kb:nvda+control+windows+downArrow" + ) + def script_moveByObjectDown(self,gesture): + self.moveMouseByObject(GCMouseDown) + + @scriptHandler.script( + # Translators: Input help message for a Golden Cursor command. + description=_("Moves the Mouse pointer up to the adjacent object"), + gesture="kb:nvda+control+windows+upArrow" + ) + def script_moveByObjectUp(self,gesture): + self.moveMouseByObject(GCMouseUp) + + def moveMouseByObject(self, direction): + w, h = api.getDesktopObject().location[2:] + x, y = winUser.getCursorPos() + if direction== GCMouseRight: + # The mouse will jump by 5 pixels in this range. + movementRange= range(x, w, 5) + movingIndex= 0 # HORIZONTAL movement + restrictionBackStep= -5 + elif direction== GCMouseLeft: + movementRange= range(x, 0, -5) + movingIndex= 0 + restrictionBackStep= 5 + elif direction== GCMouseDown: + movementRange= range(y, h, 5) + movingIndex= 1 # vertical movement + restrictionBackStep= -5 + elif direction== GCMouseUp: + movementRange= range(y, 0, -5) + movingIndex= 1 + restrictionBackStep= 5 + # Current mouse object + currentObj= self.getMouse() + for pixelValue in movementRange: + point= (pixelValue, y) if movingIndex==0 else (x, pixelValue) + newObj= currentObj.objectFromPoint(*point) + # The first different object we encounter + if newObj != currentObj: + break + else: + # loop ended without breaking + wx.Bell() + newX, newY= point + if self.restriction and self.getAppRestriction.appModule.appName != newObj.appModule.appName: + # change point from tuple to list, so we can mutate it. + point= list(point) + # Move 5 pixels back to stay in restricted application. + point[movingIndex]=point[movingIndex]+restrictionBackStep + wx.Bell() + # Move mouse to the new position. + setMousePosition(*point) + if config.conf["goldenCursor"]["reportNewMouseCoordinates"]: + x,y= point + ui.message(str(x) if movingIndex==0 else str(y)) + def getMouse(self): return api.getDesktopObject().objectFromPoint(*winUser.getCursorPos()) diff --git a/readme.md b/readme.md index 6fa7fdd..3d26a08 100755 --- a/readme.md +++ b/readme.md @@ -17,6 +17,7 @@ This add-on allows you to move the mouse using a keyboard and save mouse positio * Windows+NVDA+P: report mouse position. * Windows+NVDA+M: sswitch mouse arrows on or off. * Windows+NVDA+arrow keys (or just arrow keys if mouse arrows is on): move mouse. +* Windows+NVDA+control+arrow keys (or just control+arrow keys if mouse arrows is on): move mouse to adjacent object. Note: these gestures can be reassigned via NVDA's Input Gestures dialog under Golden Cursor category.