I'm trying to set an InputGesture
on a RoutedUICommand
, hooking it up to catch when the user presses Ctrl + =
. I'm using a KeyGesture
object, but I can't see an entry in the System.Windows.Input.Key
enum for the equals ('=') key.
我试图在RoutedUICommand设置一个输入手势,当用户按Ctrl + =时,我将其挂起以捕获。我使用的是KeyGesture object,但是我看不到System.Windows.Input中的条目。等于('=')键的键枚举。
What I was expecting was to be able to do the following:
我所期望的是能够做到以下几点:
ZoomIn = new RoutedUICommand("Zoom In", "ZoomIn", typeof(Window),
new InputGestureCollection {
new KeyGesture(Key.Equals, ModifierKeys.Control)
});
Can anyone point me in the right direction, please?
谁能告诉我正确的方向吗?
3 个解决方案
#1
2
As pointed out by ChrisF, I needed to engage my brain a little. For what it's worth, the value generated when handling the KeyDown
event is Key.OemPlus
.
正如ChrisF指出的,我需要让我的大脑参与进来。无论如何,在处理KeyDown事件时生成的值是Key.OemPlus。
Update:
One consequence of this is that, if you're doing the same same as me and you're going to use the command in a menu, you'll probably want to use the overloaded constructor for KeyGesture
that takes a 3rd parameter of displayString
. For example:
更新:这样做的一个后果是,如果您和我做的是一样的,并且您将在菜单中使用命令,那么您可能希望使用重载的构造函数来处理KeyGesture,它接受第三个displayString参数。例如:
new KeyGesture(Key.Equals, ModifierKeys.Control, "Ctrl+=")
Otherwise, you'll see the keyboard shortcut displayed as (in my case) "Ctrl+OemPlus", which isn't exactly desirable. Admittedly, the above still isn't great, but it's better than "OemPlus".
否则,您会看到键盘快捷键显示为(在我的例子中)"Ctrl+OemPlus"这并不是我们想要的。诚然,上面的内容并不好,但比“OemPlus”要好。
#2
2
One hack is to catch the shift keydown, and if the OemPlus keydown comes before the shift keyup, you have a "OemEqual". The code may look like this:
一种方法是捕捉shift键向下,如果OemPlus键向下在shift键向上之前出现,那么就有一个“OemEqual”。代码可能是这样的:
private bool shift = false;
private void Window_KeyDown(object sender, KeyEventArgs e)
{
Key key = e.Key;
switch (key) {
case Key.LeftShift: this.shift = true; break;
case Key.RightShift: this.shift = true; break;
case Key.C: this.helper.Command(CMD.CLEAR); break;
case Key.Enter: this.helper.Command(CMD.EVAL); break;
case Key.OemMinus: this.helper.Operator(OP.SUB); break;
case Key.OemPlus:
if (this.shift) {
this.helper.Operator(OP.ADD);
} else {
this.helper.Command(CMD.EVAL);
} break;
case Key.OemPeriod: this.helper.Number(NumberPad.DECIMAL); break;
case Key.D0: this.helper.Number(NumberPad.ZERO); break;
case Key.D1: this.helper.Number(NumberPad.ONE); break;
case Key.D2: this.helper.Number(NumberPad.TWO); break;
:
}
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
Key key = e.Key;
switch (key) {
case Key.LeftShift: this.shift = false; break;
case Key.RightShift: this.shift = false; break;
}
}
#3
0
if i test it, i get Key.Unknown
as the key code, and PlatformKeyCode=
= 0x000000bb
(187)
如果我测试它,我就会得到密钥。未知作为密钥代码,而PlatformKeyCode= 0x000000bb (187)
#1
2
As pointed out by ChrisF, I needed to engage my brain a little. For what it's worth, the value generated when handling the KeyDown
event is Key.OemPlus
.
正如ChrisF指出的,我需要让我的大脑参与进来。无论如何,在处理KeyDown事件时生成的值是Key.OemPlus。
Update:
One consequence of this is that, if you're doing the same same as me and you're going to use the command in a menu, you'll probably want to use the overloaded constructor for KeyGesture
that takes a 3rd parameter of displayString
. For example:
更新:这样做的一个后果是,如果您和我做的是一样的,并且您将在菜单中使用命令,那么您可能希望使用重载的构造函数来处理KeyGesture,它接受第三个displayString参数。例如:
new KeyGesture(Key.Equals, ModifierKeys.Control, "Ctrl+=")
Otherwise, you'll see the keyboard shortcut displayed as (in my case) "Ctrl+OemPlus", which isn't exactly desirable. Admittedly, the above still isn't great, but it's better than "OemPlus".
否则,您会看到键盘快捷键显示为(在我的例子中)"Ctrl+OemPlus"这并不是我们想要的。诚然,上面的内容并不好,但比“OemPlus”要好。
#2
2
One hack is to catch the shift keydown, and if the OemPlus keydown comes before the shift keyup, you have a "OemEqual". The code may look like this:
一种方法是捕捉shift键向下,如果OemPlus键向下在shift键向上之前出现,那么就有一个“OemEqual”。代码可能是这样的:
private bool shift = false;
private void Window_KeyDown(object sender, KeyEventArgs e)
{
Key key = e.Key;
switch (key) {
case Key.LeftShift: this.shift = true; break;
case Key.RightShift: this.shift = true; break;
case Key.C: this.helper.Command(CMD.CLEAR); break;
case Key.Enter: this.helper.Command(CMD.EVAL); break;
case Key.OemMinus: this.helper.Operator(OP.SUB); break;
case Key.OemPlus:
if (this.shift) {
this.helper.Operator(OP.ADD);
} else {
this.helper.Command(CMD.EVAL);
} break;
case Key.OemPeriod: this.helper.Number(NumberPad.DECIMAL); break;
case Key.D0: this.helper.Number(NumberPad.ZERO); break;
case Key.D1: this.helper.Number(NumberPad.ONE); break;
case Key.D2: this.helper.Number(NumberPad.TWO); break;
:
}
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
Key key = e.Key;
switch (key) {
case Key.LeftShift: this.shift = false; break;
case Key.RightShift: this.shift = false; break;
}
}
#3
0
if i test it, i get Key.Unknown
as the key code, and PlatformKeyCode=
= 0x000000bb
(187)
如果我测试它,我就会得到密钥。未知作为密钥代码,而PlatformKeyCode= 0x000000bb (187)