none
Mouse don't move like I wish RRS feed

  • Question

  • Hi, I can't see what is the problem in the following script. At my home computer, I can make my mouse to move to the potition I want in the screen and I can see my current position and color. At another computer, the computer don't want to move the mouse and I can see where is my mouse and current color. Can you tell me what could be the problem to make my mouse move please? If there is another method, I would try it.

    -Timer1 Working very well on both computer

    -Button1 don't work on the second computer.

    Script:

    Public Class Form1
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim bmp As New Bitmap(1, 1)
            Using g As Graphics = Graphics.FromImage(bmp)
                g.CopyFromScreen(Windows.Forms.Cursor.Position, New Point(0, 0), New Size(1, 1))
            End Using
            Dim pixel As Drawing.Color = bmp.GetPixel(0, 0)
            TextBox3.Text = pixel.Name
            TextBox4.Text = Cursor.Position.X
            TextBox5.Text = Cursor.Position.Y
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Cursor = New Cursor(Cursor.Current.Handle)
            Cursor.Position = New Point(TextBox1.Text, TextBox2.Text)
        End Sub
    
    End Class

    Thanks.

    Wednesday, June 14, 2017 10:36 PM

Answers

  • Hi

    It may be because you are trying to set the cursor position to 2 string values.

    Here is one way to do it.

    A Function that will return a valid integer from an input string.

        Private Function GetInteger(s As String) As Integer
            Dim v As Integer = 0
            If Integer.TryParse(s, v) Then Return v
            Return 0
        End Function

    and to use it:

    Cursor.Position = New Point(GetInteger(TextBox1.Text), GetInteger(TextBox2.Text))
    be aware that if you try to use string values that do not produce an integer, then you would end up with the cursor position set to (0,0)


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, June 14, 2017 11:20 PM
    • Marked as answer by HolyGODZ Sunday, June 18, 2017 6:53 PM
    Wednesday, June 14, 2017 11:18 PM
  • According to documentation, setting the cursor positions requires corresponding rights.

    Perhaps there are some differences (Operating Systems, using remote connection, etc.). Give some details. Try logging with administrative or enough rights.

    The example Holy shows works here as is. I did some corrections below.

    I think Viorel may be on to it.

    FYI I tried to make it not work and could not. I don't see where it is anything else but permission or something on the company system.

    'set mouse point position, show pointer screen color
    
    Public Class Form2
        Private WithEvents timer1 As New Timer With {.Enabled = True, .Interval = 100}
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer1.Tick
            Dim bmp As New Bitmap(1, 1)
            Using g As Graphics = Graphics.FromImage(bmp)
                g.CopyFromScreen(Cursor.Position, New Point(0, 0), New Size(1, 1))
            End Using
            Dim pixel As Color = bmp.GetPixel(0, 0)
            TextBox3.Text = pixel.ToString
            TextBox3.BackColor = pixel
            TextBox4.Text = Cursor.Position.X.ToString
            TextBox5.Text = Cursor.Position.Y.ToString
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim x1, y1 As Integer
            If IsNumeric(TextBox1.Text) Then
                x1 = CInt(TextBox1.Text)
                y1 = CInt(TextBox2.Text)
            Else
                x1 = Me.Left
                y1 = Me.Top
            End If
            Cursor.Position = New Point(x1, y1)
        End Sub
    End Class

    • Marked as answer by HolyGODZ Sunday, June 18, 2017 6:53 PM
    Thursday, June 15, 2017 12:07 PM
  • With the computer I create, it work. On the other computer, it just messes up on anything. the command "Enter" not work, "Thread.Sleep" look to freeze the computer.

    This has nothing to do with your original question, and should be asked in a new thread.

    A statement such as
      time = TextBox6.Text

    could produce results that are quite different than what you expect.   In this case the behaviour of the program is very dependant on the timer tick period, the times used for the thread delays and the active application.  Be sure to provide this detail when you ask the question.

    • Marked as answer by HolyGODZ Sunday, June 18, 2017 6:52 PM
    Saturday, June 17, 2017 10:01 PM

All replies

  • Hi

    It may be because you are trying to set the cursor position to 2 string values.

    Here is one way to do it.

    A Function that will return a valid integer from an input string.

        Private Function GetInteger(s As String) As Integer
            Dim v As Integer = 0
            If Integer.TryParse(s, v) Then Return v
            Return 0
        End Function

    and to use it:

    Cursor.Position = New Point(GetInteger(TextBox1.Text), GetInteger(TextBox2.Text))
    be aware that if you try to use string values that do not produce an integer, then you would end up with the cursor position set to (0,0)


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, June 14, 2017 11:20 PM
    • Marked as answer by HolyGODZ Sunday, June 18, 2017 6:53 PM
    Wednesday, June 14, 2017 11:18 PM
  • Can you tell me what could be the problem to make my mouse move please?

    You need to indicate what you mean by 'don't work'.  Your code is very dependant on the values in text box 1 and text box 2, so you should show what those values are.  Is the code in the button click event executing (insert a breakpoint in the method, and confirm it gets hit)?  Is there a reason for creating a new cursor?  If the cursor disappears when you run this code then the new cursor is the likely problem.  See:
    https://vbdotnetblog.wordpress.com/overview/debugging/

    Wednesday, June 14, 2017 11:23 PM
  • Thanks for those reply.

    leshay, I will try it tomorrow see if it gonna work.

    Acamar, I don't have any error message and when I click on the button when textbox1 and 2 are filled with value "exemple: 400 and 650" then it just don't move, no blink, nothing happen.

    I'm working on a macro for my job and at my home it work. At my job, the mouse just don't move.

    Thanks.


    • Edited by HolyGODZ Wednesday, June 14, 2017 11:34 PM Something left in message
    Wednesday, June 14, 2017 11:32 PM
  • I don't have any error message and when I click on the button when textbox1 and 2 are filled with value "exemple: 400 and 650" then it just don't move, no blink, nothing happen.

    What was the result when you examined the code using the debugger?  Was the code in the button click event executed?  What values are loaded into the Position property?  You can't work towards a solution without knowing the details of your problem.

    Why are you creating a new cursor?  That seems unnecessary for your task.

    Thursday, June 15, 2017 1:32 AM
  • According to documentation, setting the cursor positions requires corresponding rights.

    Perhaps there are some differences (Operating Systems, using remote connection, etc.). Give some details. Try logging with administrative or enough rights.

    Thursday, June 15, 2017 7:28 AM
  • First of all put option strict on in top of your code

    Secondly tell what are the tick intervals, as it is now the cursor is probably above the button and got its values from there. 

    Also review your program, I doubt that your intention was that you would type in a new cursor position. 


    Success
    Cor


    Thursday, June 15, 2017 9:20 AM
  • According to documentation, setting the cursor positions requires corresponding rights.

    Perhaps there are some differences (Operating Systems, using remote connection, etc.). Give some details. Try logging with administrative or enough rights.

    The example Holy shows works here as is. I did some corrections below.

    I think Viorel may be on to it.

    FYI I tried to make it not work and could not. I don't see where it is anything else but permission or something on the company system.

    'set mouse point position, show pointer screen color
    
    Public Class Form2
        Private WithEvents timer1 As New Timer With {.Enabled = True, .Interval = 100}
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer1.Tick
            Dim bmp As New Bitmap(1, 1)
            Using g As Graphics = Graphics.FromImage(bmp)
                g.CopyFromScreen(Cursor.Position, New Point(0, 0), New Size(1, 1))
            End Using
            Dim pixel As Color = bmp.GetPixel(0, 0)
            TextBox3.Text = pixel.ToString
            TextBox3.BackColor = pixel
            TextBox4.Text = Cursor.Position.X.ToString
            TextBox5.Text = Cursor.Position.Y.ToString
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim x1, y1 As Integer
            If IsNumeric(TextBox1.Text) Then
                x1 = CInt(TextBox1.Text)
                y1 = CInt(TextBox2.Text)
            Else
                x1 = Me.Left
                y1 = Me.Top
            End If
            Cursor.Position = New Point(x1, y1)
        End Sub
    End Class

    • Marked as answer by HolyGODZ Sunday, June 18, 2017 6:53 PM
    Thursday, June 15, 2017 12:07 PM
  • Yeah, its probably OS or security configuration related.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, June 15, 2017 12:11 PM
    Moderator
  • Yeah, its probably OS or security configuration related.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Why?

    AFAIK is Cursor just a .Net class on a very high abstraction level which inherit from Windows.forms.. 


    Success
    Cor

    Thursday, June 15, 2017 12:19 PM
  • Yeah, its probably OS or security configuration related.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Why?

    AFAIK is Cursor just a .Net class on a very high abstraction level which inherit from Windows.forms.. 


    Success
    Cor

    Cor,

    Oh just because I cant think of anything else. And cant reproduce the problem here.

    Plus long ago a forum member told me set cursor position could be considered malicious or used in software with bad intent. Perhaps they can turn it off?


    PS Oh sorry, I thought that was to me.
    Thursday, June 15, 2017 12:27 PM
  • Yeah, its probably OS or security configuration related.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Why?

    AFAIK is Cursor just a .Net class on a very high abstraction level which inherit from Windows.forms.. 


    Success
    Cor

    Oh just because I cant think of anything else. And cant reproduce the problem here.

    Plus long ago a forum member told me set cursor position could be considered malicious or used in software with bad intent. Perhaps they can turn it off?

    Correct.  There is software which can block cursor automation from other software.  Typically we see this in anti-cheating software for games, but I can't say that some security product isn't doing something similar.

    And as Viorel pointed out, there are security considerations at the OS level.

    When a project works on one computer but fails on another, the most obvious place to look is at differences between the two computers.  OS, security configuration, and security software are probably the top three differences to look at.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, June 15, 2017 12:32 PM
    Moderator

  • Correct.  There is software which can block cursor automation from other software.  Typically we see this in anti-cheating software for games, but I can't say that some security product isn't doing something similar.

    And as Viorel pointed out, there are security considerations at the OS level.


    If that exist then there is certainly a link which shows that. By the way, we are talking here about a cursor inside a windows forms application. Not one everywhere on the screen.


    I've never seen that or heard of that before you know. 


    Success
    Cor

    Thursday, June 15, 2017 1:04 PM
  •  By the way, we are talking here about a cursor inside a windows forms application. Not one everywhere on the screen.


    I've never seen that or heard of that before you know. 


    Success
    Cor

    Its the whole screen. Its picking the color under the pointer for the whole screen and setting position based on screen cords.

    I have never heard of it either (software to block set cursor pos). But...



    Thursday, June 15, 2017 1:08 PM

  • Its the whole screen. Its picking the color under the pointer for the whole screen and setting position based on screen cords.

    I have never heard of it either (software to block set cursor pos). But...



    You're right about that whole screen Tommy, nevertheless I'm probably likewise you curious where Reed got his knowledge about that blocked cursor. 

    Success
    Cor

    Thursday, June 15, 2017 1:15 PM
  • If that exist then there is certainly a link which shows that. By the way, we are talking here about a cursor inside a windows forms application. Not one everywhere on the screen.


    I've never seen that or heard of that before you know. 


    Success
    Cor

    Cor,

    Just because you haven't heard of something, that doesn't mean that it does not exist.  And I am getting weary of being asked to defend myself whenever you do not know what I am talking about.  So I will do it once more, but also ask that in the future you look into it yourself and find evidence that I am wrong before asking me to defend my statements.

    https://en.wikipedia.org/wiki/Valve_Anti-Cheat

    https://www.battleye.com/about/

    There's two links to about the most information you are going to get.  No, there is not "certainly a link which shows that" because these products are super-secretive about what exactly they do and how exactly they do it.

    I was once a heavy MMO gamer.  I have a lot of experience with cheaters and the anti cheating software to defeat them, and I have done white-hat automation applications of my own in VB to help look for additional ways to cheat the game so that I could report them.

    I have personally encountered a situation where the mouse position could not be set while a particular game was running.  As soon as you end the exe, the code works as expected.

    While I have not encountered this type of behavior with a system security suite, I would not be so bold as to say that means no security suite has decided to adopt some of this anti-cheating technology in order to mitigate damages in a remote-takeover hacking scenario.

    Anyway, while its more likely to be an OS or group policy type of issue, the fact that other software could be interfering is worth mentioning and I only interjected because Tommy brought it up with some uncertainty and I wanted to confirm his memory.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, June 15, 2017 1:23 PM
    Moderator
  • Reed,

    This is what you wrote and where I asked if you had documentation about. 

    Yeah, its probably OS or security configuration related.

    In what you present I could find nothing that shows that the OS or security configuration related thing blocks a mouse move. Not anti virus software, but even that should be well known and wide written on Internet because this OP would not be the only one than having this problem.. 


    And yes I was also a heavy gamer, that does not make that I use this forum to tell things which I think it was in a game. I'm an MVP you know and one of the things which is expected from those is that they only tell things based on verifiable sources.

    As MVP I've a responsibility you know. I should not lead persons in the wrong direction in a way that they can write: "A Microsoft MVP wrote it". 


    Success
    Cor

    Thursday, June 15, 2017 2:32 PM
  • Reed,

    This is what you wrote and where I asked if you had documentation about. 

    Yeah, its probably OS or security configuration related.

    ...
    Cor

    My apologies for the confusion.

    This one is easier.

    http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Cursor.cs,9b794e9d30341224

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms648394(v=vs.85).aspx

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms687391(v=vs.85).aspx

    So setting Cursor.Position involves the access control model, which means any software which can manipulate the access control, or any difference in its configuration (due to OS defaults or security policy configuration) could in theory prevent the property setting from taking effect.

    This was all meant to be an aside, not an in-depth conversation.  As Acamar initially, and then yourself later, pointed out there is no need to create a new cursor and this may be where the issue is arising.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, June 15, 2017 2:47 PM
    Moderator
  • Hi all, Thanks for everything. Strangely, I can make it work now. I hope it will work everytime now...

    But I will follow that closely. If anything don't work again, I will be back.

    Thanks to your answer, I will take a look if I can learn more on what you wrote here.

    Friday, June 16, 2017 11:11 AM
  • Hi all, Thanks for everything. Strangely, I can make it work now. I hope it will work everytime now...

    But I will follow that closely. If anything don't work again, I will be back.

    Thanks to your answer, I will take a look if I can learn more on what you wrote here.

    Thanks for letting us know.

    I guess you should mark your post as the answer to close the thread unless you have other questions.

    Friday, June 16, 2017 12:10 PM
  • Yeah another question. this happen on the same computer it not work. I think there is something blocking or something left in the computer. This time, the computer freeze a little and it do half of the script.

    Is there any requirement to run well a microsoft visual basic program?

    Imports System.Threading
    Public Class Form1
    
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            Dim time As Integer
            Dim timeTwo As Integer
            time = TextBox6.Text
            timeTwo = TextBox7.Text
            My.Computer.Keyboard.SendKeys("^{a}")
            Thread.Sleep(time)
            My.Computer.Keyboard.SendKeys("^{v}")
            Thread.Sleep(timeTwo)
            My.Computer.Keyboard.SendKeys("{enter}")
            Thread.Sleep(time)
            My.Computer.Keyboard.SendKeys("{end}")
            Thread.Sleep(time)
            My.Computer.Keyboard.SendKeys("{up}")
            Thread.Sleep(time)
            My.Computer.Keyboard.SendKeys("{end}")
            Thread.Sleep(time)
            My.Computer.Keyboard.SendKeys("{down}")
            Thread.Sleep(time)
            My.Computer.Keyboard.SendKeys("^{c}")
            Thread.Sleep(time)
            Label6.Text = My.Computer.Clipboard.GetText()
            Thread.Sleep(time)
            My.Computer.Keyboard.SendKeys("^{a}")
            Thread.Sleep(time)
            My.Computer.Keyboard.SendKeys("{del}")
            Thread.Sleep(100)
            My.Computer.Keyboard.SendKeys("%{tab}")
            Timer2.Enabled = False
        End Sub

    With the computer I create, it work. On the other computer, it just messes up on anything. the command "Enter" not work, "Thread.Sleep" look to freeze the computer.

    Thanks again...

    Saturday, June 17, 2017 9:55 PM
  • With the computer I create, it work. On the other computer, it just messes up on anything. the command "Enter" not work, "Thread.Sleep" look to freeze the computer.

    This has nothing to do with your original question, and should be asked in a new thread.

    A statement such as
      time = TextBox6.Text

    could produce results that are quite different than what you expect.   In this case the behaviour of the program is very dependant on the timer tick period, the times used for the thread delays and the active application.  Be sure to provide this detail when you ask the question.

    • Marked as answer by HolyGODZ Sunday, June 18, 2017 6:52 PM
    Saturday, June 17, 2017 10:01 PM