开发者

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

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 tri开发者_如何转开发ed many variations but I can't seem to find the answer, thanks.


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


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


Its quite simple:

SendKeys.SendWait("%({tab})");


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜