how to perform a hold ALT+TAB sendkey event in C#

25,482

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

Share:
25,482
mendez
Author by

mendez

Updated on July 09, 2022

Comments

  • mendez
    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
      Joe Enos over 12 years
      Isn'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
    mendez over 12 years
    isn't there a way to achieve this by doing the following System.windows.forms.sendkeys.send("ALT")+("TAB") or something like that?
  • mendez
    mendez over 12 years
    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 autohotkey.com/docs/commands/Send.htm
  • Jesus Ramos
    Jesus Ramos over 12 years
    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.
  • mendez
    mendez over 12 years
    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?
  • Jesus Ramos
    Jesus Ramos over 12 years
    I 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
    mendez over 12 years
    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.
  • Jesus Ramos
    Jesus Ramos over 12 years
    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)
  • Peter Turner
    Peter Turner about 9 years
    this doesn't work for me in PowerShell 4, I get Exception calling "SendWait" with "1" argument(s): "Specified repeat count is not valid."
  • Gallus
    Gallus about 6 years
    You need to load the Assemblies add-type -AssemblyName microsoft.VisualBasic add-type -AssemblyName System.Windows.Forms
  • Istvan Heckl
    Istvan Heckl almost 5 years
    System.Windows.Forms.SendKeys.SendWait("%{Tab}"); worked for me
  • obachtos
    obachtos over 3 years
    This 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
    Chandra Prakash Variyani almost 3 years
    that's not working only for alt tab. everything else is working, any idea why?