How to press Alt+Tab programmatically in C# in win 8 - c#

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}");

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

How to click on a "pane" using UI automation library?

We have an application in which I need to click on a Pane. I tried to use the following code, which I use to click on a button, but it gave Unsupported pattern exception.
InvokePattern click_pattern = (InvokePattern)adjust_button.GetCurrentPattern(InvokePattern.Pattern);
click_pattern.Invoke();
Is there any other way to do it?
Even though the object is clickable, depending on how the click is being handled internally, you may not necessarily be able to use the InvokePattern to perform a click. That appears to be the case here.
As an alternative, you can use some code to move the mouse cursor over the pane and issue a click using P/Invoke. Something like this:
private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
[DllImport("user32.dll")]
private static extern void mouse_event(UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, IntPtr dwExtraInfo);
...
...
AutomationElement paneToClick;
...
...
Cursor.Position = paneToClick.GetClickablePoint();
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new IntPtr());
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new IntPtr());

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)

C# Simulate Key Press

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

How to simulate keypress

I know how to use SendKeys() but how do i go about it if i would like to simulate holding ESCAPE key for like 5 seconds?
You can PInvoke keybd_event and hold down Escape key for 5 seconds and then release it:
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
keybd_event(VK_ESCAPE, 0, 0, 0) // KEY_DOWN
System.Threading.Thread.Sleep(5000);
keybd_event(VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0) // KEY_UP
try using a timer... use System.Forms.Timer... for 5000ms... then if the 5000ms is finished, shut the timer off..

Categories

Resources