How to check current mouse button state using Win32/User32 library? - c#

I know how to stimulate clicks using User32 SendInput method and what I need is a similar User32 method but to obtain the current mouse button state.
Something similar to:
public static extern bool GetCursorPos(ref System.Drawing.Point lpPoint);
Function GetCursorPos gives me the current cursor position. What I need is the left button state (if it's clicked or not). Is there such a function?

Use GetAsyncKeyState, To Quote MSDN:
The GetAsyncKeyState function works
with mouse buttons. However, it checks
on the state of the physical mouse
buttons, not on the logical mouse
buttons that the physical buttons are
mapped to. For example, the call
GetAsyncKeyState(VK_LBUTTON) always
returns the state of the left physical
mouse button, regardless of whether it
is mapped to the left or right logical
mouse button. You can determine the
system's current mapping of physical
mouse buttons to logical mouse buttons
by calling
GetSystemMetrics(SM_SWAPBUTTON).

There's a method called GetAsyncKeyState. The method signature looks like this:
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(UInt16 virtualKeyCode);
Then you simply call it passing the left mouse key code (VK_LBUTTON = 0x01) and off you go.
More information directly from MSDN.

Related

Restrict Mouse Move only on a Specified Area

private void MoveCursor()
{
// Set the Current cursor, move the cursor's Position,
// and set its clipping rectangle to the form.
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
Cursor.Clip = new Rectangle(this.Location, this.Size);
}
I am using the above code to Restrict the movement but still I am able to move the mouse outside the form?
Can I restrict the mouse movement to a specified area in the form? Please advise...
Updated answer:
ClipCursor is the API function you require. You will need to supply screen-based coordinates.
BOOL WINAPI ClipCursor(RECT *lpRect);
take a look at this link for the Win32 API code, and this one for pinvoke from C#.
There is pair of Win32 API functions called SetCapture/ReleaseCapture that will restrict the mouse to a certain window bounds.
You will need to use PInvoke to use them, but this will work.
[DllImport("user32.dll")]
static extern IntPtr SetCapture(long hWnd);
SetCapture(Control.Handle);
One thing to bear in mind, is that if used incorrectly, it's possible that the user will not be able to click the [X] to shut down your application because the mouse will not be able to get to the title bar.

alternate moving /hightlighting a control using x/y postioning

I'm looking for an api that can cause a windows select/highlight event to occur on a windows desktop, without actually causing the mouse cursor to move.. I can cause the mouse cursor to move with :
public static extern bool SetCursorPos(int X, int Y);
But that moves the actual cursor to that point... I'm looking for a way to highlight only as one might do by using the tab and arrow keys to move around the windows desktop. Any suggestions are appreciated..
regards, rob
I think you may be looking for SetFocus. You can get a control's handle with Control.Handle or FindWindow , and p/invoke SetFocus (use IntPtr as the argument type).

Mouse.GetPostion(null) Equivalent in WinRT

What would be the equivalent for Mouse.GetPostion(null) which is in WPF.
I can get the position of pointer from the pointer event args. but how do i get the position without any event. like "Mouse.GetPostion(null)" in WPF. where mouse is a static class.
Any idea?
There is a PointerPoint.Position property - PointerPoint is a general input abstraction that also inludes touch and stylus input so you have to get a focus on mouse input first - this can be done with a static
PointerPoint PointerPoint.GetCurrentPoint(uint pointerId)
method where pointerId is a system generated number identifying the input device. I don't know if there is any other way but you can get the mouse PointerId through
PointerRoutedEventArgs.Pointer.PointerId
if you handle some mouse Pointer event like PointerPressed or PointerReleased first.

How to determine whether the left mouse button is pressed?

In a winform, the form will follow the mouse when the mouse is down, but sometimes when the machine particularly is slow, the form is following the mouse even the mouse is out, so I used win32 dll to judge the state of the mouse
[DllImport("user32.dll")]
static extern short GetAsyncKeyState(int nVirtKey);
public static bool GetCurrentLeftMouseIsDowning()
{
if (GetAsyncKeyState(0x01) == 0)
return false;
else
return true;
}
But the memory will increase when the form has been dragging, and what other way to determine the state of the mouse?Or how to control the memory when the form is dragged??
Considering you are using winform, you can use form1_mousedown event for this
refer this

SendMessage simulated action is working when mouse is being moved only. Is there a way to solve that?

I'm working on a 'drag' action in game'ss window. I want to mouse_down in a one place and then mouse_up another one.
Here's the c# code:
uint Iparm = makeDWord((ushort)x, (ushort)y);
IntPtr xd = new IntPtr(Iparm);
uint Iparm2 = makeDWord((ushort)x2, (ushort)y2);
IntPtr xd2 = new IntPtr(Iparm2);
SendMessage(p, (uint)0x201, (IntPtr)0x1, xd); // down button (start x,y)
SendMessage(p, (uint)0x202, (IntPtr)0x0, xd2); // up button (final x,y)
I have connected that code with a button (lets say button named START) in a c# form. When I push START and keep on moving my mouse, it works perfectly. But when I just push START and wait for the action (without moving the cursor on the screen), nothing happens. I've checked game's window with spy++ while pushing button in my form, and there's no difference between two ways of pushing that button (moving/not moving cursor).
I've forgotten to add that while moving cursor, action takes place even though window is minimized (what is very cool)
What's causing my problem ? : P
edit:
WM_MOUSEDOWN = 0x201;
WM_MOUSEUP = 0x202;
That code is placed in a function, p is windows handle, button START executes that function.
As I said before, the code works just fine while I'm moving cursor
Try calling SendMessage with a WM_MOUSEMOVE message in between the down and up.
Use SendInput instead.

Categories

Resources