I have two monitors, one of which is a touch screen device. I want to prevent the mouse from moving to the touch screen device when a touch event is triggered.
So , I use Hook_LL
As long as this object exists all mouse events created from a touch event for legacy support will be disabled. Or, at least, that's the promise. But the system mouse-cursor still gets set to the position of the last touch-event, and so far, there seems to be no way to prevent that.
static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
var info = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
// 64-bit
var extraInfo = (ulong)info.dwExtraInfo.ToInt64();
if ((extraInfo & MOUSEEVENTF_MASK) == MOUSEEVENTF_FROMTOUCH)
{
// Prevent Touch -> Mouse Event message delivery.
return new IntPtr(1); // if ((extraInfo & 0x80) != 0) is Touch Input,other is Pen Input.
}
}
return UnsafeNativeMethods.CallNextHookEx(hookId, nCode, wParam, lParam);
}
I use GetCursorPos that the scheme was effective, even though the mouse disappeared.
Unfortunately, when the touch down is triggered, I don't move the mouse, just click the left mouse button, but click down is still triggered on the touch screen device.
Although I can disable click down triggering on the touch screen device, I want to respond to click down at the position of the mouse.
I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.
I hope that after clicking the touch screen button, when clicking with the mouse, the click event will respond to the original position instead of the button on the touch screen.
How to reset mouse cursor position c#
How to avoid mouse move on Touch
Related
I'm developing a C Windows forms application and I need to block every input from the keyboard and the mouse while the user is using the stylus on a Wacom tablet.
I've tried using global mouse and keyboard hooks as described here to manage all the mouse and keyboard events generated by Windows and blocking the ones not generated by the stylus.
Everything is OK for the keyboard, but I'm not able to tell the source of mouse events. I've tried this kind of approach:
// [DllImport( "user32.dll" )]
// private static extern uint GetMessageExtraInfo( );
uint extra = GetMessageExtraInfo();
bool isPen = ( ( extra & 0xFFFFFF00 ) == 0xFF515700 );
as described here, but it doesn't work, since the value of extra is always 4283912448, reghardless of the source of the event.
Is there something I'm doing wrong?
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
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.
i need to implement a cursor with some very specific features:
it has to be animated
because after n seconds it automatically clicks - so the animation is feedback for the user when the click will happen
it has to snap to some of our controls
it has to work outside of our application
the approaches so far:
render my WPF-control into a bitmap, make a cursor-struct out of it and use user32.dll/SetSystemCursor to set it
PRO
the cursor has no delay after the mouse since it's a real cursor
CON
snapping is quite hard, especially since we have absolute and relative inputdevices and i would have to reset the mouseposition all the time or use user32.dll/ClipCursor (System.Windows.Forms.Cursor.Clip does the same) but the snapped cursor is always shaking around the snapped position (tries to escape, get's reset again....)
the code i use throws strange exceptions after some random time - so my current code seems quite unstable
render my own cursor into a maximized, topmost, allowtransparent, windowstyle=none, invisible window and manually move the cursor after the mouse (like Canvas.SetLeft(cursor, MousePosition.X))
PRO
snapping can be (easily) done
CON
when the mouse clicks and hit's the cursor the cursor get's clicked and not the window beyond
polling the mouseposition in a dispatcher-background-loop all the time doesn't seem very beautiful to me
to solve the second approach my cursor would have to have at least one transparent pixel
in the hotspot, so that the mouse can click through... that doesn't seem like a real solution to me...
any idea's anyone?
EDIT:
some example-source to show the problems...:
example app & source to show the problem with snapping the mouse to a fixed position: ClipIt.rar
example app & source that fails after random time - setting a self-drawn cursor: TryOwnCur.rar
can be found under: http://sourcemonk.com/Cursor
thanks to http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a3cb7db6-5014-430f-a5c2-c9746b077d4f
i can click through my self-drawn cursor which follows the mouse-position by
setting the window style:none, and allowtransparent as i already did and
then
public const int WS_EX_TRANSPARENT = 0x00000020;
public const int GWL_EXSTYLE = (-20);
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hwnd,
int index);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hwnd,
int index, int newStyle);
public static void makeTransparent(IntPtr hwnd) {
int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
}
and call makeTransparent from OnSourceInitialized...
I know how to simulate mouse and keyboard events, but they act as if the user did them, so they will affect the window that is active. What I need is to simulate one of those inputs, but in a Window that is not active.
I'm not saying that it is minimized, imagine for example, you have msPaint, and notepad. Notepad is in front of paint. And you want to simulate mouse clicks in certain coordinates of the paint window, but without setting it active, making it possible for the user to keep using notepad which is in fron of paint.
Is this possible at all? Thanks!
I've tried this:
const int WM_KEYDOWN = 0x100;
Thread.Sleep(5000);
Process x = Process.Start(#"C:\WINDOWS\NOTEPAD.EXE");
PInvokes.PlatformInvokeUSER32.SendMessage(x.MainWindowHandle, WM_KEYDOWN, ((int)Keys.W), 0);
but it doesn't work =( Doesn't do anything :(
You can try the UI automation API. It supports also legacy applications.
Maybe try the PostMessage function instead:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern int PostMessage(
int hWnd, int msg, int wParam, IntPtr lParam);
Depending on the target app, there could be lots of issues. Some apps trigger on WM_KEYDOWN or WM_KEYUP instead of WM_CHAR. Also, the key might already be down and is being ignored. For targets like webbrowsers, you need to get the right window handle. Winspector can get that for you.
Your code wont work because SendMessage requires that the window you're sending to be active.
Try PostMessage.