UWP: Simulate click at specific coordinates on Windows IoT - c#

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.

Related

Simulate Keyboard key with keybd_event to UWP

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

Make Form Top Most But Not Over Other Applications

In vb.net or c# WinForms, how would you make a form topmost over the other forms in the project, but not over the windows of other applications?
Using form.topmost = True puts the form above other applications.
EDIT
I am NOT looking for a splash screen.
Below is an example of the intended behavior of this form. It remains on top of everything else in the application, and you can interact with it and the form behind it.
The topmost=true should work fine for your application. There must be a user error occurring.
To bring a form on top of other forms withon an application, you can use the BringToFront method.
Application.OpenForms["MyForm"].BringToFront();
The other forms will be accessible to the user.
You can use the SetWindowPos method to bring a window to the front without activating it. You could call this in a timer to keep it on top (but that will probably put it in front of other apps, so you would only want to do that if you were the activate application) or you would have to detect when other forms fire the Activated event and then call this.
internal const int SWP_NOMOVE = 0x0002;
internal const int SWP_NOSIZE = 0x0001;
internal const int SWP_SHOWWINDOW = 0x0040;
internal const int SWP_NOACTIVATE = 0x0010;
internal const int SWP_NOOWNERZORDER = 0x0200;
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
Call it with:
SetWindowPos(form.Handle,0,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW|SWP_NOACTIVATE);

C# app that simulates mouse click [duplicate]

This question already has answers here:
How do you simulate Mouse Click in C#?
(7 answers)
Closed 7 years ago.
Does anyone knows how to perform a mouse click on a specific point of the desktop? I need a application to perform mouse click on another's application button localized in a specific place on desktop.
This works in WinForms
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
public void DoMouseClick() {
//Call the imported function with the cursor's current position
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
From http://www.gamedev.net/topic/321029-how-to-simulate-a-mouse-click-in-c/
You may look into Coded UI. It's main purpose is for performing UI tests on applications and supports programmatic access to a variety of different application frameworks (WPF, WinForms, VB, etc).

Move Mouse to Position and Left Click

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)]

Simulate mouse clicks at a certain position on inactive window in C#

Here is the original question but it is considered to java:
Simulate mouse clicks at a certain position on inactive window in Java?
Anyways, I'm building a bot to run in the background. This bot requires me to click. Of course, I want to be able to do other things while the bot is running.
So I was wondering if it was possible for me to simulate a mouse click at a certain position on an inactive window.
If this is possible, I would greatly appreciate it if any of you could help me.
Thanks!
Yes it is possible, here's the code I used for a previous school project:
[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;
//This simulates a left mouse click
public static void LeftMouseClick(Point position)
{
Cursor.Position = position;
mouse_event(MOUSEEVENTF_LEFTDOWN, position.X, position.Y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, position.X, position.Y, 0, 0);
}
EDIT : It seems that the mouse_event function was replaced by SendInput() but it still works (Windows 7 and earlier)

Categories

Resources