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?
Related
I have the task of automating some forms fill in web browser java application.
But Java have protection from such automation. It has protection from filling data using AutoIt. Because of this protection sometimes data is not filling correctly in Java form and automation process is therefore impossible.
Then I tried to use Microsoft virtual keyboard and it works good with Java forms. However it also have protection against AutoIt and when I try to generate mouse clicks on virtual keyboard using AutoIt this doesn't work. Keys are simply not pressed.
Therefore I am looking to somehow write little exe file that will just generate single mouse click, but on hardware level. This file should always run in background and do not steel focus from active window. This will allow me to use virtual keyboard to fill java forms.
I have found http://glovepie.org/ but I can not find the way to pro grammatically call left mouse click. I only can remap some keyboard key to mouse click with GlovePIE.
Also I read about keydown and keyrelease events in C++ But I am afraid that Virtual keyboard might have protection against it. Also I do not know how to write proper C++ code to generate mouse click using keydown and keyrelease.
EDIT
#include <windows.h>
#include <Winuser.h>
int __stdcall WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int cmdShow)
{
void click(){
INPUT input = { 0 }; //INPUT structure
// left down
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; //left mouse button down
SendInput(1, &input, sizeof(INPUT)); //call SendInput, pass in input to click
Sleep(1);
// left up
ZeroMemory(&input, sizeof(INPUT)); //clear out input
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_LEFTUP; //left mouse button up
SendInput(1, &input, sizeof(INPUT)); //call SendInput, pass in input to unclick
}
}
void click(){
INPUT input={0}; //INPUT structure
// left down
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; //left mouse button down
SendInput(1,&input,sizeof(INPUT)); //call SendInput, pass in input to click
Sleep(1);
// left up
ZeroMemory(&input,sizeof(INPUT)); //clear out input
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_LEFTUP; //left mouse button up
SendInput(1,&input,sizeof(INPUT)); //call SendInput, pass in input to unclick
}
This is C++ Windows API code that clicks and releases the left mouse button. It's the lowest level you probably want to deal with.
The MSDN documentation on SendInput might also be of use: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx
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).
I need to get an input point (caret position, window/control) that is focused. My application/service needs to detect when the user starts typing and then replace the characters that were typed with predetermined values. The trick is that I don't know where the user wants to type(I don't want to limit this to several applications).
I think that I know how to get/replace the text using:
[DllImport("USER32.DLL", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wparam, StringBuilder text);
How do I get the control that is focused? How do I know from where to get the text and where to send the replacement?
I am doing this in C#, WPF.
Thank you!
If you just want to emulate keyboard activity, you can use keybd_event or SendInput. These automatically deliver the keypresses to the window with focus, so you don't have to detect it yourself.
I am presuming that since you have tagged your question as WinAPI your are trying to intercept keystrokes from another application as in Keyboard Hooking. Take a look at these Links:
Intercepting and Blocking Keystrokes
How do I set a low level mouse hook and keyboard hook in C#?
How to set a Windows hook in Visual C# .NET
A Simple C# Global Low Level Keyboard Hook
So, I thought this would be simple and, well, I was wrong. Here is a simplified description of the problem:
I am writing a small application for our manufacturing folks that will grab a screenshot of the entire desktop as well as the foreground window when they click the application's icon in the system tray. Currently, I am using the Win32 method "GetforegroundWindow" in the MouseMove event of the NotifyIcon to save the foreground window handle and take the screenshot in the Click event.
This does work sometimes, but if I click the icon very quickly I actually capture the task bar instead of the foreground window. I am not sure why this is happening (I do understand that the task bar is a window, I don't understand why sometimes it seems to have focus in MouseMove before I have clicked), and I have had little luck using the EnumWindows method as well, likely because I do not completely understand how it works.
It would seem that, if I were able to get the z position of each window using only the window handle, this would be an easy problem to solve using EnumWindows. I have not found a method to do that however.
So, I ask you guys; how would you write a method to locate the foreground window reliably, given that it may not have focus at the time? Either my google-fu is failing me or the information on this is sparse. Thanks in advance.
The taskbar is as valid a foreground window as any. When you click it, it will temporarily be the foreground window. And if you click Start and press Escape for example, it will be the foreground window until you click off of it.
You can probably use GetWindow with HWND_NEXT passing in the window handle of the taskbar.
Nevermind, since the taskbar is a topmost window, GetWindow (or GetNextWindow, etc) will operate differently. I would suggest revisiting the EnumWindows solution which is probably your best bet.
If the form you want the snapshot of is the same as the form linked to the task bar, you really do not need to use GetforegroundWindow. Just use the Form.hWnd and pass that into the function getting the snapshot. You may need to make it the top window by calling
[DllImport( "user32.dll" )]
public static extern IntPtr SetForegroundWindow( IntPtr hWnd );
or
[DllImport( "user32.dll" )]
public static extern bool BringWindowToTop( HandleRef hWnd );
If you want the whole desktop, then you probably just need to put in a Thread.Sleep to make sure the foreground window has had enough to come to the top before getting the desktop snapshot.
putting the src from my comment here for better printing
[DllImport( "user32.dll" )]
public static extern IntPtr GetForegroundWindow();
[DllImport( "user32.dll" )]
public static extern IntPtr GetActiveWindow();
// this or the next line not both
IntPtr curWindow = GetActiveWindow();
IntPtr curWindow = GetForegroundWindow();
BringWindowToTop( window );
System.Threading.Thread.Sleep( 500 );
Where the thread spleep gives the window enough time to come to the top of the Z order.
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.