4

I'd like to run a script every time I move my mouse.

This is the first step in making a workaround so that I can use 100% of my monitor edges to go between them.

Is this possible?

2
  • 1
    Yes, it is possible to create a workaround. Create a script that loops with some delay. Use eg. xdotool getmouselocation --shell to get the edge. use some if statements to check if you are hitting edge multiple times- If true, use eg. xdotool mousemove to move the pointer onto the other screen. It's not the most efficient way but it will work. Feb 14, 2015 at 0:53
  • There are several ways. It depends on what you exactly want to do with it, and in what language you are using it. Feb 21, 2015 at 22:50

1 Answer 1

6

The question literally

Detecting mouse movement, can be done with several tools. Two examples:

  1. Using xdotool in a script:
    [you'd need xdotool to be installed]:

    sudo apt-get install xdotool
    
    • Compare (in a loop) two mouse positions, with a time interval:
      The command to get the mouseposition from xdotool:

      $ xdotool getmouselocation
      x:1449 y:137 screen:0 window:79691788
      
    • If we parse this output in a (python) script, we can let it run a command if the mouse moves (if position1 does not equal position2):

      #!/usr/bin/env python3
      import subprocess
      import time
      
      def get_mousepos():
          curr = subprocess.check_output(["xdotool", "getmouselocation"]).decode("utf-8")
          return [int(it.split(":")[1]) for it in curr.split()[:2]]
      
      current1 = get_mousepos()
      while True:
          time.sleep(0.5)
          current2 = get_mousepos()
          if not current1 == current2:
              # run a command:
              print("action")
          current1 = current2
      
  2. Using (python's) tkinter The exact same process can also be done with python's own tkinter:

    [you'd need tkinter3 to be installed]:

    sudo apt-get install python3-tk
    
    • The script, using tkinter:

      #!/usr/bin/env python3
      from tkinter import*
      import time
      
      root = Tk()
      
      def current_position():
          return [root.winfo_pointerx(), root.winfo_pointery()]
      
      pos1 = current_position()
      while True:
         time.sleep(0.5)
          pos2 = current_position()
          if not pos1 == pos2:
              # run a command:
              print("action!")
          pos1 = pos2
      
      root.mainloop()
      

An example related to what you are aiming at

An example of what can be done to control what command to run when the mouse trespasses a certain position (line) below.

#!/usr/bin/env python3
import subprocess

def get_mousepos():
    curr = subprocess.check_output(["xdotool", "getmouselocation"]).decode("utf-8")
    return [int(it.split(":")[1]) for it in curr.split()[:2]]

current1 = get_mousepos()
while True:
    current2 = get_mousepos()
    # make the jump depend on the position and direction:
    jump = 100 if current2[1] < 525 else -100
    if current2[0] < 1000 and current1[0] >= 1000:
        print("gone left") # you can run any other command here, like:
        subprocess.Popen(["xdotool", "mousemove", str(current2[0]), str(current2[1]+jump)])
    elif current2[0] > 1000 and current1[0] <= 1000:
        print("gone right") # you can run any other command here
        subprocess.Popen(["xdotool", "mousemove", str(current2[0]), str(current2[1]-jump)])
    current1 = current2

In this example, the mouse makes a jump when you move it from left to right or vice versa:

enter image description here

In this case, the mouse jumps a fixed jump. However, throwing some mathematics in, and move the jump line to the right side of your screen (+ a separate "arrangement" for the top left of the second screen), you can make the jump proportionally, to create the setup that you where aiming at in your first question: make (in a dual screen setup) both screens seem of the same vertical resolution when moving the mouse from one screen to another.

The "dead corner" where you cannot move the mouse from one screen to another would not exist then. (I have a working situation on my two screens)

It would still have a strange effect with windows if they are are spread over the two screens however, but it can be done.

enter image description here

1
  • @Jack was this the kind of answer you were looking for? Feb 26, 2015 at 14:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .