I need to simulate keyboard pressure and release on an Univesal Windows App.
I tried this code on a wpf and it works, but using the same code on a UWP does not work.
Is it a limit for windows app or is there any other problem?
Thanks.
[DllImport("user32.dll")]
static extern void keybd_event(byte key, byte scan, int flags, int extraInfo);
const int KEYEVENTF_KEYUP = 0x2;
const byte KEY_A = 0x41 //A character
public static void simulateKeyDown(KEY_A)
{
keybd_event(KEY_A, 0, 0, 0);
}
public static void simulateKeyUp(KEY_A)
{
keybd_event(KEY_A, 0, KEYEVENTF_KEYUP, 0);
}
In a UWP app you have to use the Input Injection APIs and capability for simulating keyboard (and mouse, touch, etc.).
https://learn.microsoft.com/en-us/uwp/api/windows.ui.input.preview.injection
Related
Long long time ago (in a galaxy far away), I used to program some fun tools to assist me (as having a disability) or just support my lazyness :)
Tools that do things like "wait 30 seconds and then press play on my media player" or "save a list of all song names from winamp that streaming a live m3u based radio". it was almost 20 years ago, using C# and window messages api (not wanting to relay on mouse clicks and strict window size&position). I would've found the window's handle and the "control" handle and interact with it.
The question is: Can I still do it today in the age of Windows 10?
If so, how?
I would appreciate a starting point.
Let's say I want to press play on my bs.player after x seconds, or close an error message that comes up every 10 seconds (well, its not cause' my windows is healthy ..but theoretically).
Thank you :)
Yes you can, if you talk about Windows API.
You need to declare the external WinAPI's signatures as static extern using the DllImport attribute.
For example to know if the screen saver is active or if an app runs in full screen:
private const int WM_SYSCOMMAND = 0x0112;
private const int SC_SCREENSAVE = 0xF140;
private const int SPI_GETSCREENSAVERRUNNING = 0x0072;
[DllImport("user32.dll", SetLastError = true)]
static extern bool SystemParametersInfo(int action, int param, ref int retval, int updini);
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
static private extern bool GetWindowRect(HandleRef hWnd, [In, Out] ref RECT rect);
[DllImport("user32.dll")]
static private extern IntPtr GetForegroundWindow();
static private bool IsForegroundFullScreen()
{
return IsForegroundFullScreen(null);
}
static private bool IsForegroundFullScreen(Screen screen)
{
if ( screen == null ) screen = Screen.PrimaryScreen;
RECT rect = new RECT();
GetWindowRect(new HandleRef(null, GetForegroundWindow()), ref rect);
return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top)
.Contains(screen.Bounds);
}
private bool IsScreensaverActive()
{
int active = 1;
SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, ref active, 0);
return active != 0;
}
private bool IsForegroundFullScreenOrScreensaver()
{
return IsForegroundFullScreen() || IsScreensaverActive();
}
Calling Win32 DLLs in C#
c# dllimport with pointers
Is there a way to simulate a click at specific coordinates on Windows IoT?
I tried with mouse_event:
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
but, I get this error:
System.EntryPointNotFoundException: 'Unable to find an entry point named 'mouse_event' in DLL 'user32.dll'.'.
Is it because that function doesn't exist in the IoT version of Windows?
I saw there was SendInput, but the only syntax on the documentation is in C++. Is it possible at all to use it in C# on Windows IoT and if so how? If you have an example in mind, linking it would be very helpful. I searched around but I couldn't find something that could work on UWP.
Here is the code I used for the mouse_event:
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetCursorPos")]
private static extern bool SetCursorPos(int X, int Y);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
//...
public void Click(int x, int y)
{
SetCursorPos(x,y);
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
As of Fall Creators Update Release of Windows IOT (SDK version 16299), you can use the InputInjector API in UWP apps: https://learn.microsoft.com/en-us/uwp/api/Windows.UI.Input.Preview.Injection.InputInjector
The API allows injecting mouse input among other input types.
Neither mouse_event nor SendInput can be used in a UWP application. As documented, these API's are for
desktop apps only
If you are running this from a UWP application (which apparently you are), there is no way to automate another UWP application. The sandboxing will not allow this. This includes UI Automation.
I use this code in VS 2013 in win8 for simulate press Alt+Tab,
but nothing happen.
i test it in win 7 and VS 2012 it's work fine.
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
private const byte VK_MENU = 0x12;
private const byte VK_TAB = 0x09;
private const int KEYEVENTF_EXTENDEDKEY = 0x01;
private const int KEYEVENTF_KEYUP = 0x02;
keybd_event(VK_MENU, 0xb8, 0, 0); //Alt Press
keybd_event(VK_TAB, 0x8f, 0, 0); // Tab Press
System.Threading.Thread.Sleep(70);
keybd_event(VK_TAB, 0x8f, KEYEVENTF_KEYUP, 0); // Tab Release
keybd_event(VK_MENU, 0xb8, KEYEVENTF_KEYUP, 0); // Alt Releas
How can i solve this problem in win8?
thanks
may be its too late to answer but an answered question may help other in future.
Try to send key combination using SendKeys it may work for you.
SendKeys.Send("%+{TAB}");
I'm working on an Windows Form Application in C#, Framework 4 (32 bit).
I have a list that holds coords of the mouse, and I can capture them. So far so good.
But at some point, I want to go to those coords and left mouse click on it.
This is how it looks like right now:
for (int i = 0; i < coordsX.Count; i++)
{
Cursor.Position = new Point(coordsX[i], coordsY[i]);
Application.DoEvents();
Clicking.SendClick();
}
And the Clicking class:
class Clicking
{
private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
private static extern void mouse_event(
UInt32 dwFlags, // motion and click options
UInt32 dx, // horizontal position or change
UInt32 dy, // vertical position or change
UInt32 dwData, // wheel movement
IntPtr dwExtraInfo // application-defined information
);
// public static void SendClick(Point location)
public static void SendClick()
{
// Cursor.Position = location;
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
}
}
But I'm getting this error:
Could not load type 'program.Clicking' from assembly 'program, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'mouse_event' has no implementation (no RVA).
And i realy don't understand what the problem is... Do you guys know what the problem is? or do you know an better way to do what i'm trying to do?
Have you included the following line?
[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData,
UIntPtr dwExtraInfo);
This will import the function mouse_event from the user32 dll, which is what you are trying to use within your program. Currently your program does not know about this method within the DLL untill you specify wher it comes from.
The website PInvoke.net user32 Mouse Event is quite handy for the basics on this sort of thing.
The answer to Directing mouse events [DllImport(“user32.dll”)] click, double click will be of great help to your understanding as well.
The flags are what commands you want to send into the mouse_input function, in that example you can see that he is sending both mouse down and mouse up in the same line, this is fine because the mouse_event function will split those flags up and execute them consecutively.
Also note that this method has been superseded by the SendInput command, a good example of SendInput and SetMousePos can be found At this Blog
I guess you are missing the following line
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
I was looking for a way to simulate pressing the right Ctrl key in C#, it must be the right one. I know this can be done for the left one but I couldn't find anything on the right one. It is so I can simulate the key press for the manually triggered bsod.
Thanks
You can use keybd_event event to simulate right Ctrl key press.
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag
public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
public const int VK_RCONTROL = 0xA3; //Right Control key code
Usage:
keybd_event(VK_RCONTROL, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_RCONTROL, 0, KEYEVENTF_KEYUP, 0);
For other key simulation here is virtual key codes list.
You might have some luck with the Windows Input Simulator http://inputsimulator.codeplex.com/
If you are usign AutoHotKey try looking here. with {RControl} you should get what you want
Update: For .NET try looking at this for more info, but AFAIK you can't send right Ctrl key. guess you must use win32 to accomplish it