数字键盘除号 {DIVIDE} 若要指定与 Shift、Ctrl 和 Alt 键的任意组合一起使用的键,请在这些键代码之前加上以下一段或多段代码。键 代码
Shift +
Ctrl ^
Alt % 若要指定在按一些键时应同时按住 Shift、Ctrl 和 Alt 键的任意组合,请将这些键的代码放在括号内。例如,若要指定按 E 和 C 时同时按住 Shift 键,请使用“+(EC)”。若要指定按 E 时应按住 Shift,之后按 C 时不按住 Shift,请使用“+EC”。若要指定重复键,请使用格式 {key number}。键和数字之间必须放一空格。例如,{LEFT 42} 表示按向左键 42 次;{h 10} 表示按 H 10 次。注意 因为没有可用来激活另一个应用程序的托管方法,所以可以在当前应用程序中使用此类,或使用本机 Windows 方法(如 FindWindow 和 SetForegroundWindow)来强制聚焦到其他应用程序上。 示例 下面的代码示例演示如何使用 Send 方法。若要运行该示例,请将下面的代码粘贴到包含一个名为 Button1 的按钮的窗体 Form1 中。请确保该 Click 事件已与此示例中的事件处理方法关联。应将该按钮控件的 TabIndex 属性设置为 0。当该示例运行时,双击该窗体以触发按钮的 Click 事件。 ' Clicking Button1 causes a message box to appear. Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click MessageBox.Show("Click here!") End Sub ' Use the SendKeys.Send method to raise the Button1 click event ' and display the message box. Private Sub Form1_DoubleClick(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.DoubleClick ' Send the enter key; since the tab stop of Button1 is 0, this ' will trigger the click event. SendKeys.Send("{ENTER}") End Sub // Clicking Button1 causes a message box to appear. private void Button1_Click(System.Object sender, System.EventArgs e) { MessageBox.Show("Click here!"); } // Use the SendKeys.Send method to raise the Button1 click event // and display the message box. private void Form1_DoubleClick(object sender, System.EventArgs e) { // Send the enter key; since the tab stop of Button1 is 0, this // will trigger the click event. SendKeys.Send("{ENTER}"); } .NET Framework 安全性 SecurityPermission ,它允许非托管代码调用此方法。关联的枚举:SecurityPermissionFlag.UnmanagedCode
public class Form1 : Form
{
public Form1()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.ResumeLayout(false);
} private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
SendKeys.Send("%{TAB}");
}
}
}
如果LZ想要全局级的按键模拟,那还得些个全局的Hook去捕捉按键就可以了。
向活动应用程序发送击键。 命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)语法
Visual Basic(声明)
Public Shared Sub Send ( _
keys As String _
)
Visual Basic(用法)
Dim keys As StringSendKeys.Send(keys)
C#
public static void Send (
string keys
)
参数
keys
要发送的击键字符串。 异常
异常类型 条件
InvalidOperationException
没有可向其发送击键的活动应用程序。
ArgumentException
keys 并不表示有效击键
备注
每个键都由一个或多个字符表示。若要指定单个键盘字符,请使用该字符本身。例如,若要表示字母 A,请将字符串“A”传递给方法。若要表示多个字符,请将各个附加字符追加到它之前的字符的后面。若要表示字母 A、B 和 C,请将参数指定为“ABC”。加号 (+)、插入符号 (^)、百分号 (%)、波浪号 (~) 以及圆括号 ( ) 对 SendKeys 具有特殊含义。若要指定这些字符中的某个字符,请将其放在大括号 ({}) 内。例如,若要指定加号,请使用“{+}”。若要指定大括号字符,请使用“{{}”和“{}}”。中括号 ([ ]) 对 SendKeys 没有特殊含义,但必须将它们放在大括号内。在其他应用程序中,中括号具有特殊含义,此含义可能会在发生动态数据交换 (DDE) 时起重要作用。警告
如果应用程序用于键盘各不相同的国际用途,则使用 Send 可能产生不可预知的结果,应避免使用它。
若要指定在按键(如 Enter 或 Tab)时不显示的字符,以及表示操作而不表示字符的键,请使用下表中的代码:键
代码
Backspace
{BACKSPACE}、{BS} 或 {BKSP}
Break
{BREAK}
Caps Lock
{CAPSLOCK}
Del 或 Delete
{DELETE} 或 {DEL}
向下键
{DOWN}
End
{END}
Enter
{ENTER} 或 ~
Esc
{ESC}
Help
{HELP}
Home
{HOME}
Ins 或 Insert
{INSERT} 或 {INS}
向左键
{LEFT}
Num Lock
{NUMLOCK}
Page Down
{PGDN}
Page Up
{PGUP}
Print Screen
{PRTSC}(保留供将来使用)
向右键
{RIGHT}
Scroll Lock
{SCROLLLOCK}
Tab
{TAB}
向上键
{UP}
F1
{F1}
F2
{F2}
F3
{F3}
F4
{F4}
F5
{F5}
F6
{F6}
F7
{F7}
F8
{F8}
F9
{F9}
F10
{F10}
F11
{F11}
F12
{F12}
F13
{F13}
F14
{F14}
F15
{F15}
F16
{F16}
数字键盘加号
{ADD}
数字键盘减号
{SUBTRACT}
数字键盘乘号
{MULTIPLY}
数字键盘除号
{DIVIDE}
若要指定与 Shift、Ctrl 和 Alt 键的任意组合一起使用的键,请在这些键代码之前加上以下一段或多段代码。键
代码
Shift
+
Ctrl
^
Alt
%
若要指定在按一些键时应同时按住 Shift、Ctrl 和 Alt 键的任意组合,请将这些键的代码放在括号内。例如,若要指定按 E 和 C 时同时按住 Shift 键,请使用“+(EC)”。若要指定按 E 时应按住 Shift,之后按 C 时不按住 Shift,请使用“+EC”。若要指定重复键,请使用格式 {key number}。键和数字之间必须放一空格。例如,{LEFT 42} 表示按向左键 42 次;{h 10} 表示按 H 10 次。注意
因为没有可用来激活另一个应用程序的托管方法,所以可以在当前应用程序中使用此类,或使用本机 Windows 方法(如 FindWindow 和 SetForegroundWindow)来强制聚焦到其他应用程序上。
示例
下面的代码示例演示如何使用 Send 方法。若要运行该示例,请将下面的代码粘贴到包含一个名为 Button1 的按钮的窗体 Form1 中。请确保该 Click 事件已与此示例中的事件处理方法关联。应将该按钮控件的 TabIndex 属性设置为 0。当该示例运行时,双击该窗体以触发按钮的 Click 事件。
' Clicking Button1 causes a message box to appear.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("Click here!")
End Sub
' Use the SendKeys.Send method to raise the Button1 click event
' and display the message box.
Private Sub Form1_DoubleClick(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.DoubleClick ' Send the enter key; since the tab stop of Button1 is 0, this
' will trigger the click event.
SendKeys.Send("{ENTER}")
End Sub // Clicking Button1 causes a message box to appear.
private void Button1_Click(System.Object sender, System.EventArgs e)
{
MessageBox.Show("Click here!");
}
// Use the SendKeys.Send method to raise the Button1 click event
// and display the message box.
private void Form1_DoubleClick(object sender, System.EventArgs e)
{ // Send the enter key; since the tab stop of Button1 is 0, this
// will trigger the click event.
SendKeys.Send("{ENTER}");
} .NET Framework 安全性
SecurityPermission ,它允许非托管代码调用此方法。关联的枚举:SecurityPermissionFlag.UnmanagedCode
还是不行啊请问如何模拟出按下alt键不动,同时按tab键
{
public Form1()
{
InitializeComponent(); }
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern void keybd_event(VirtualKeyType bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
const uint KEYEVENTF_KEYUP = 0x02;
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.A)
{
keybd_event(VirtualKeyType.VK_MENU, 0x45, 0, UIntPtr.Zero);
for (int i = 0; i < 3; ++i)
{
keybd_event(VirtualKeyType.VK_TAB, 0x45, 0, UIntPtr.Zero);
System.Threading.Thread.Sleep(1000);
}
keybd_event(VirtualKeyType.VK_TAB, 0x45, KEYEVENTF_KEYUP, UIntPtr.Zero);
keybd_event(VirtualKeyType.VK_MENU, 0x45, KEYEVENTF_KEYUP, UIntPtr.Zero);
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
public enum VirtualKeyType
{
VK_TAB = 0x09,
VK_SHIFT = 0x10,
VK_CONTROL = 0x11,
VK_MENU = 0x12, //ALT KEY
VK_LSHIFT = 0xA0,
VK_RSHIFT = 0xA1,
VK_LCONTROL = 0xA2,
VK_RCONTROL = 0xA3,
VK_LMENU = 0xA4,
VK_RMENU = 0xA5
}
更多的VirtualKeyType
http://homepage2.nifty.com/qta/work/vk_code.htm
private void txtMessage_KeyDown(object sender, KeyEventArgs e)
{
e.Alt && e.KeyCode == Keys.Tab
{
//doing something here.
}
}希望对你有所帮助!