Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27441

Set the cursor position from a mouse hook.

$
0
0
I'm trying to build a quick program (or so I thought) that would lock the Y axis of your mouse when holding down a key on the keyboard, in this case the space bar.

Saw the idea from a member here and thought it'd be fun to do. Turns out the last step has been fighting me tooth and nail.


It's within the mouse hooked method (while space is being pressed) that I call Cursor.Position = New Point(Cursor.Position.X, Y). Y being the variable locked when the space key was pressed down. This should and actually does work. The problem I'm seeing has to be something with these hooks. Because you can see that it is in fact setting the cursor position then going back. It's just happening so fast you can barely catch it. My guess is it's setting the cursor to where I want, then setting it back to where it should be (when the mouse movement occurred).

is there any way around this, or am I just doing something wrong?



vbnet Code:
  1. Private Function MouseProc(ByVal nCode As Int32, ByVal wParam As Int32, ByRef lParam As MSLLHOOKSTRUCT) As IntPtr
  2.     If (nCode = HookCommand.ACTION) Then
  3.         If LockPosition AndAlso wParam = WindowsMessage.MOUSEMOVE Then
  4.             Cursor.Position = New Point(Cursor.Position.X, Y)
  5.             'SetCursorPos(x,y) ' no effect, still the same results.
  6.         End If
  7.     End If
  8.     Return CallNextHookEx(MouseHook, nCode, wParam, lParam)
  9. End Function

Viewing all articles
Browse latest Browse all 27441

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>