how to perform a hold ALT+TAB sendkey event in C#
Solution 1
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.
Solution 2
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}");
Solution 3
Its quite simple:
SendKeys.SendWait("%({tab})");
Solution 4
[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 ;)
mendez
Updated on July 09, 2022Comments
-
mendez almost 2 years
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.
-
Joe Enos over 12 yearsIsn't Alt-Tab a special Windows key combo that can't be messed with? I'd expect it to be similar to Ctrl-Alt-Del.
-
-
mendez over 12 yearsisn't there a way to achieve this by doing the following System.windows.forms.sendkeys.send("ALT")+("TAB") or something like that?
-
mendez over 12 yearsI 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 autohotkey.com/docs/commands/Send.htm
-
Jesus Ramos over 12 yearsIn 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.
-
mendez over 12 yearsyeah 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?
-
Jesus Ramos over 12 yearsI don't think so, use this as a reference pinvoke.net I've not tried it with WinForms but I believe it won't let you do special keystrokes.
-
mendez over 12 yearsthanks 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.
-
Jesus Ramos over 12 yearsHope you got this working (SendKeys with pinvoke can be kind of tricky with all the keycodes but the site contains a lot of information)
-
Peter Turner about 9 yearsthis doesn't work for me in PowerShell 4, I get
Exception calling "SendWait" with "1" argument(s): "Specified repeat count is not valid."
-
Gallus about 6 yearsYou need to load the Assemblies
add-type -AssemblyName microsoft.VisualBasic add-type -AssemblyName System.Windows.Forms
-
Istvan Heckl almost 5 yearsSystem.Windows.Forms.SendKeys.SendWait("%{Tab}"); worked for me
-
obachtos over 3 yearsThis 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. -
Chandra Prakash Variyani almost 3 yearsthat's not working only for alt tab. everything else is working, any idea why?