I am trying to use a sendkey event that holds the ALT key and then presses the TAB key. How do you perform that action, I've tried many variations but I can't seem to find the answer, thanks.
5 Answers
After going through the MSDN documentation page I came up with this and it seems to be working just fine for me:
SendKeys.Send("%{Tab}");

- 1,036
- 2
- 21
- 31
-
1
-
[This](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/how-to-simulate-mouse-and-keyboard-events-in-code?view=netframeworkdesktop-4.8&redirectedfrom=MSDN) might also be helpful, especially when dealing with various windows version. I had to do the `app.config` modification to get consitent behaviour on an older machine. – obachtos Dec 22 '20 at 15:10
-
that's not working only for alt tab. everything else is working, any idea why? – Chandra Prakash Variyani May 07 '21 at 16:01
[Windows.Forms.Sendkeys]::SendWait("%{Alt Down}")
[Windows.Forms.Sendkeys]::SendWait("%{TAB}")
[Windows.Forms.Sendkeys]::SendWait("%{Alt Up}")
Works in Powershell for me! Thanks for tips ;)

- 31
- 2
-
2this doesn't work for me in PowerShell 4, I get `Exception calling "SendWait" with "1" argument(s): "Specified repeat count is not valid."` – Peter Turner Mar 30 '15 at 17:03
-
You need to load the Assemblies `add-type -AssemblyName microsoft.VisualBasic add-type -AssemblyName System.Windows.Forms` – Gallus Feb 20 '18 at 15:15
Using sendkeys PInvoke it's possible to do this by sending ALT keydown event, TAB keydown, then TAB keyup, then ALT keyup. There is also another way using the ALT modifier on the key but I cannot remember exactly how as I haven't worked with it in a while.
If you want to do multiple tabs alternate the TAB keydown and keyup while keeping the ALT on keydown.

- 22,940
- 10
- 58
- 88
-
isn't there a way to achieve this by doing the following System.windows.forms.sendkeys.send("ALT")+("TAB") or something like that? – mendez Aug 05 '11 at 05:05
-
I found this article about using the sendkey events I tried using System.Windows.Forms.SendKeys.Send("{Alt Down}{TAB}{Alt Up}"); and blind in my code but when ever I try to run the program it gives me a error saying that that function doesn't exist. any suggestions? take a look at the article http://www.autohotkey.com/docs/commands/Send.htm – mendez Aug 05 '11 at 18:16
-
In Windows Forms I am not sure, I am really only familiar with the SendKeys from the native C++ library that you can call using PInvoke. This one allows you to send any combination you want. Also that article is specifically for using AutoHotkey. – Jesus Ramos Aug 05 '11 at 21:01
-
yeah that's what I was hoping for, I want my program to use the alt+tab event to switch focus to another program automatically. I don't really like to use any third party tools to do my programing are you sure there isn't another way? – mendez Aug 06 '11 at 01:57
-
I don't think so, use this as a reference http://pinvoke.net/ I've not tried it with WinForms but I believe it won't let you do special keystrokes. – Jesus Ramos Aug 06 '11 at 02:01
-
thanks for the tip, I have another question but i think the question is another subject so ill ask it in another forum thanks again all. – mendez Aug 06 '11 at 06:03
-
Hope you got this working (SendKeys with pinvoke can be kind of tricky with all the keycodes but the site contains a lot of information) – Jesus Ramos Aug 06 '11 at 06:05
I could not get the SendKeys.Send to switch programs. Ricky Lee's answer here worked for me.
using System.Runtime.InteropServices;
[DllImport("user32")]
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;
private void button1_Click(object sender, System.EventArgs e)
{
keybd_event(VK_MENU,0,0,0);
keybd_event(VK_TAB,0,0,0);
System.Threading.Thread.Sleep(1000);
keybd_event(VK_TAB,0,0,0);
System.Threading.Thread.Sleep(1000);
keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0);
keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0);
}

- 160
- 8