为可自定义的键盘快捷键创建输入
我正在使用 Visual Studio 2010 创建一个可视化 C# 应用程序,并且我希望在我的应用程序的首选项中包含一些选项,以便使用某种文本框输入自定义键盘快捷键。我了解如何记录键盘输入,以及如何将其保存到用户应用程序设置,但我找不到任何具有此功能的输入控件。
即类似这样的内容:
但是使用 Windows 表单(注意:以上内容来自 Divvy for OS X,来自应用商店)。
是否有任何内置功能可以处理这个问题? 我可以使用任何好的库或自定义输入吗?
否则,关于如何实施这样的事情有什么建议吗?
解决方案:
使用Bas B的答案和其他一些逻辑:
private void fShortcut_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode != Keys.Back)
{
Keys modifierKeys = e.Modifiers;
Keys pressedKey = e.KeyData ^ modifierKeys; //remove modifier keys
if (modifierKeys != Keys.None && pressedKey != Keys.None)
{
//do stuff with pressed and modifier keys
var converter = new KeysConverter();
fShortcut.Text = converter.ConvertToString(e.KeyData);
//At this point, we know a one or more modifiers and another key were pressed
//modifierKeys contains the modifiers
//pressedKey contains the other pressed key
//Do stuff with results here
}
}
else
{
e.Handled = false;
e.SuppressKeyPress = true;
fShortcut.Text = "";
}
}
上面是一种通过检查两个修饰键和另一个键是否已输入来判断何时输入有效快捷键组合的方法。按下。
I am using Visual Studio 2010 to create a visual C# application, and I want to include some options in my application's preferences for customization of keyboard shortcuts using some sort of text box input. I understand how to record keyboard input, and how to save it to the user application settings, but I can not find any input controls that have this functionality.
I.e. something like this:
But using windows forms (Note: The above is from Divvy for OS X from the app store).
Is there any built-in functionality to handle this?
Are there any good libraries or custom inputs I could use?
Otherwise, any suggestions on how to go about implementing something like this?
Solution:
Using Bas B's answer and some other logic:
private void fShortcut_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode != Keys.Back)
{
Keys modifierKeys = e.Modifiers;
Keys pressedKey = e.KeyData ^ modifierKeys; //remove modifier keys
if (modifierKeys != Keys.None && pressedKey != Keys.None)
{
//do stuff with pressed and modifier keys
var converter = new KeysConverter();
fShortcut.Text = converter.ConvertToString(e.KeyData);
//At this point, we know a one or more modifiers and another key were pressed
//modifierKeys contains the modifiers
//pressedKey contains the other pressed key
//Do stuff with results here
}
}
else
{
e.Handled = false;
e.SuppressKeyPress = true;
fShortcut.Text = "";
}
}
The above is a way to tell when a valid shortcut combination is entered by checking if both modifier keys, and another key is pressed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以让用户在 TextBox 中输入首选快捷方式,然后处理 KeyDown 事件,例如:
编辑:更新以包含 Stecya 的答案。
You could have the user enter the preferred shortcut in a TextBox, then handle the KeyDown event, for instance:
Edit: Updated to include Stecya's answer.