C# Simulate Key Press - c#

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

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

C# PostMessage CTRL+Fx not working

I'm trying to simulate the CTRL+Fx key press in a window, but it's not working as spected. When my program send the key stroke, it just ignore the CTRL, so if it press CTRL+F3, the window gets only the F3 key press, here are some screenshots of the events captured with Window Detective:
Pressing CTRL+F3manually:
And this one is my program pressing CTRL+F3 using PostMessage:
As you can see, they are exactly the same, so I have no idea what to do anymore. Here is the code I'm using:
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint msg, uint wParam, uint lParam);
const int VK_CONTROL = 0x11;
const uint VK_CONTROL_LUP = 0xC01D0001;
const uint VK_CONTROL_LDOWN = 0x1D0001;
const int VK_F3 = 0x72;
const uint F3_LDOWN = 0x3D0001;
const uint F3_LUP = 0xC03D0001;
const int WM_KEYDOWN = 0x0100;
const int WM_KEYUP = 0x0101;
//Modifier
PostMessage(hWnd, WM_KEYDOWN, (uint)VK_CONTROL, VK_CONTROL_LDOWN);
//Key down
WinApi.PostMessage(hWnd, WM_KEYDOWN, (uint)VK_F3, F3_LDOWN);
//key up
WinApi.PostMessage(hWnd, WM_KEYUP, (uint)VK_F3, F3_LUP);
WinApi.PostMessage(hWnd, WM_KEYUP, (uint)VK_CONTROL, VK_CONTROL_LUP);
After a few tests I added a delay of 10 seconds after the first PostMessage (holding CTRL down) and if I press an arrow key the window detect the CTRL pressed, but if I press any other key, like F3, it just ignore it.
There is any other way to send key strokes to unfocused windows besides the PostMessage I'm using? Or there is some secret behind it that it might be ignored for some programs?
Thanks

LEAP Motion C# XAML WPF Application

I'm busy making a application where i can use the Leap as a mouse. I'm making a WPF application with C# and XAML.
I allready can move the cursor, but i have problems making a function to activate the left mouse button.
Can someone help me with this problem? I need to activate buttons created in XAML.
Another solution could be a function that activates a button when de Leap cursor is on the button for like 3 seconds. I can't find any examples on the Internet. Does someone have a simple basic program or example for me? Please help!
Here is a link to the application i allready have. Maybe it helps
https://www.dropbox.com/sh/kp51hdbhcacaky7/pdinDQpA-6
About the "mouse over button" thing - u can try this solution (worked for me):
create hover event for buttons (mouse entered / leave)
create a bool flag for like "isHovered" (true if hovered, false if not)
start a DispacherTimer, set it's Tick (3 sec?)
on Tick do mouse click (previous answer)
hope it halped :)
try this:
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int 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 static void DoMouseClick()
{
Point mousePoint = GetMousePosition();
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (int)mousePoint.X, (int)mousePoint.Y, 0, 0);
}

Sending a LeftClick event to a Text Box

Is there any way I can send a left click event to a TextBox? for what ever the reason although I am doing a TextBox.Focus() and the cursor is blinking inside the TextBox but I cannot start typing in it, But if I do an extra click with the mouse inside the text box then I can start typing. So, I was wondering how to send that event to it?
To send mouse events, you'll need to import user32.dll and use mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
Example
class Mouse
{
[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; //Left click
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08; //Right click
private const int MOUSEEVENTF_RIGHTUP = 0x10;
public static void sendMouseRightclick(Point p)
{
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, p.X, p.Y, 0, 0); //Sends a mouse right click at the specified Point
}
public static void sendMouseClick(Point p)
{
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, p.X, p.Y, 0, 0); //Sends a mouse left click at the specified Point
}
}
When using this, you may call Mouse.sendMouseClick(Point p) to send a mouse left click at the specified point.
In your case, I think that you might want to use Mouse.sendMouseClick(TextBox.Location); to send a mouse left click at the current TextBox position.
Thanks,
Have a great day :)
Ok to fix this first we should make sure the form itsself is activate.
To do that, in the Form_Shown event we should call this.Activate()

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

Categories

Resources