c#监听键盘组合

标签 c# winforms keyboard-events

我想编写简单的程序,在某些地方发送我的用户名和密码,例如,当我想登录网站时,我发现了this。听键盘的项目。

所以我有这个功能:

private void HookManager_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{

}

private void HookManager_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{

}

private void HookManager_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{

}

当我按下 a 键时,我得到了这样的结果:

int value = e.KeyValue; // 65
Keys key = e.KeyCode;   // A

所以我想知道如何捕捉特定的键盘组合,而不是只有一个,例如Ctrl + l

最佳答案

您可以为 KeyDown 使用这样的代码:

if(e.KeyCode == Keys.F1 && (e.Alt || e.Control || e.Shift))
{
}

基于KeyEventArgs来自 MSDN 的文档:

A KeyEventArgs, which specifies the key the user pressed and whether any modifier keys (CTRL, ALT, and SHIFT) were pressed at the same time, is passed with each KeyDown or KeyUp event.

The KeyDown event occurs when the user presses any key. The KeyUp event occurs when the user releases the key. Duplicate KeyDown events occur each time the key repeats, if the key is held down, but only one KeyUp event is generated when the user releases the key.

The KeyPress event also occurs when a key is pressed. A KeyPressEventArgs is passed with each KeyPress event, and specifies the character that was composed as a result of each key press.

关于c#监听键盘组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53679937/

相关文章:

c# - 写入文件仅在 "step in" Debug模式下有效?

c# - TweetSharp SendTweetWithMediaOptions 示例

c# - 如何使用 C# 启用和禁用特定树节点上的上下文菜单项

javascript - 如何在 IE8 中生成具有特定键码的 keyup 事件?

delphi - Windows Vista 上的应用程序看不到 Shift+Ctrl+0 组合键

c# - 使用 docker compose 的容器之间的 API 请求连接被拒绝

c# - Xamarin Forms - 如何在 ListView 中显示/绑定(bind)列表?

c# - 异常 :Failed to convert parameter value from a string to a datetime

winforms - 有没有办法为ListView制作一个加载栏(进度条)?

javascript - document.onkeydown 在 Firefox 中不起作用