6 Replies - 815 Views - Last Post: 26 August 2013 - 06:00 AM Rate Topic: -----

#1 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

Pymouse

Posted 23 August 2013 - 06:40 PM

My problem is that i want to be able to create a program that moves the mouse where i want it to go. Examples: up/down/left/right.
However all i see on the internet is code that determines where the mouse goes by the resolution of your screen.
from pymouse import PyMouse
from pykeyboard import PyKeyboard
import time

m = PyMouse()
m.move(30,500)
time.sleep(2)
m.screen_size()
m.position()


This simple code puts the cursur in the area of the screen but can't be used to just simply move it up or down. Any help will be greatly appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Pymouse

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Pymouse

Posted 24 August 2013 - 06:33 AM

You could use m.position() to make the movements relative to the mouse's current location. Let's say you wanted to move it up 10 pixels:

pos = m.position()
m.move(pos[0], pos[1] - 10)


By doing this, screen resolution itself isn't necessary. You could even take it a step further and make the movements percentage based in comparison to the screen's resolution (we'll try moving the mouse up 10% of the screen resolution):

res = m.screen_size()
pos = m.position()
ydiff = res[1] *0.1
m.move(pos[0], pos[1] - ydiff)


It all comes down on what you're trying to achieve with it, really.
Was This Post Helpful? 1
  • +
  • -

#3 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

Re: Pymouse

Posted 24 August 2013 - 08:55 AM

You sir, are a genius! The code works and moves the mouse proportional to where the cursur is located. However as i am a python noob. I have no clue how you did that. If you could explain it to me it would be great so then i could figure out how to move it left or right.
Thanks :)
Was This Post Helpful? 0
  • +
  • -

#4 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

Re: Pymouse

Posted 24 August 2013 - 01:58 PM

i have recently uncovered this problem where i can only click the mouse using m.click by giving it a set area to click. I want to be able to click wherever the mouse actually is not by setting coordinates.
Was This Post Helpful? 0
  • +
  • -

#5 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Pymouse

Posted 25 August 2013 - 10:36 AM

View PostDestroyer5, on 24 August 2013 - 11:55 AM, said:

You sir, are a genius! The code works and moves the mouse proportional to where the cursur is located. However as i am a python noob. I have no clue how you did that. If you could explain it to me it would be great so then i could figure out how to move it left or right.
Thanks :)/>


It's pretty easy, actually. Using m.position(), you get the current coordinates of the mouse, and then you do some math to the current position to make the mouse move where you want relative to its location at that time.


View PostDestroyer5, on 24 August 2013 - 04:58 PM, said:

i have recently uncovered this problem where i can only click the mouse using m.click by giving it a set area to click. I want to be able to click wherever the mouse actually is not by setting coordinates.


Also quite simple. Just feed the coordinates from m.position() to m.click():

pos = m.position()
m.click(pos[0], pos[1])


This works off the same principle described above. Instead of thinking of the implementation as using specific locations, use the results from m.position() and make it relative.
Was This Post Helpful? 0
  • +
  • -

#6 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

Re: Pymouse

Posted 25 August 2013 - 06:55 PM

Thanks so much! i was able to finally create this
from pymouse import PyMouse
from pykeyboard import PyKeyboard
import time
import speech
commands = ["Mouse Down", "Mouse all the way to the right", "Mouse all the way to the left", "Right Click", "Mouse to the left a little", "Double Click", "Mouse to the right a lot", "Mouse to the left a lot", "Mouse to the right a little", "Mouse Up", "Mouse down a little", "Mouse to the right", "Mouse Up a little", "Mouse to the left", "Click"]
print commands
def Get(phrase, listener):
    if phrase == "Mouse Down":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.move(pos[0], pos[1] - -100)
    elif phrase == "Right Click":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.click(pos[0], pos[1], 3)
    elif phrase == "Click":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.click(pos[0], pos[1])
    elif phrase == "Double Click":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.click(pos[0], pos[1])
        m.click(pos[0], pos[1])
    elif phrase == "Mouse Up":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.move(pos[0], pos[1] - 100)
    elif phrase == "Mouse Up a little":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.move(pos[0], pos[1] - 20)
    elif phrase == "Mouse down a little":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.move(pos[0], pos[1] - -20)
    elif phrase == "Mouse to the left":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.move(pos[0] - 50, pos[1])
    elif phrase == "Mouse to the right":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.move(pos[0] + 50, pos[1])
    elif phrase == "Mouse to the right a little":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.move(pos[0] + 10, pos[1])
    elif phrase == "Mouse to the right a lot":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.move(pos[0] + 200, pos[1])
    elif phrase == "Mouse all the way to the right":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.move(pos[0] + 1000, pos[1])
    elif phrase == "Mouse all the way to the left":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.move(pos[0] - 1000, pos[1])
    elif phrase == "Mouse to the left a little":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.move(pos[0] - 10, pos[1])
    elif phrase == "Mouse to the left a lot":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.move(pos[0] - 200, pos[1])
    
        
        
        

Ll = speech.listenfor(["Mouse Down", "Mouse all the way to the right", "Mouse all the way to the left", "Right Click", "Mouse to the left a little", "Double Click", "Mouse to the right a lot", "Mouse to the left a lot", "Mouse to the right a little", "Mouse Up", "Mouse down a little", "Mouse to the right", "Mouse Up a little", "Mouse to the left", "Click"], Get)





Without your help i would of been stuck in the dirt! Thanks again
Was This Post Helpful? 0
  • +
  • -

#7 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Pymouse

Posted 26 August 2013 - 06:00 AM

I noticed in lines 12 and 43 you are subtracting negative numbers. It would probably be clearer what is happening if you switched those to just adding the numbers =)

pos[1] + 100 and pos[1] + 20, respectively.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1