我对C#很陌生,并且正在一个现有的应用程序中做一些工作。我有一个DirectX视图端口,其中包含我希望能够使用箭头键定位的组件。
目前,我正在重写ProcessCmdKey,捕捉箭头输入并发送OnKeyPress事件。这是可行的,但我希望能够使用修饰符(ALT+CTRL+Shift那就是。只要我拿着一个修饰符,按箭头,就不会触发我正在听的事件。
有没有人对我该怎么做有任何想法或建议?
发布于 2018-04-04 08:07:50
如何确定已按下哪个键?Keydata(第二个参数)的值将取决于按下的键和任何修饰符键,因此,例如,按左箭头将返回代码37,移位-左将返回65573,ctrl-左131109和ALT-左262181。
您可以使用适当的枚举值提取安定按下的修饰符和键:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
bool shiftPressed = (keyData & Keys.Shift) != 0;
Keys unmodifiedKey = (keyData & Keys.KeyCode);
// rest of code goes here
}
发布于 2018-04-04 09:32:19
public Keys UnmodifiedKey(Keys key)
{
return key & Keys.KeyCode;
}
public bool KeyPressed(Keys key, Keys test)
{
return UnmodifiedKey(key) == test;
}
public bool ModifierKeyPressed(Keys key, Keys test)
{
return (key & test) == test;
}
public bool ControlPressed(Keys key)
{
return ModifierKeyPressed(key, Keys.Control);
}
public bool AltPressed(Keys key)
{
return ModifierKeyPressed(key, Keys.Alt);
}
public bool ShiftPressed(Keys key)
{
return ModifierKeyPressed(key, Keys.Shift);
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (KeyPressed(keyData, Keys.Left) && AltPressed(keyData))
{
int n = code.Text.IndexOfPrev('<', code.SelectionStart);
if (n < 0) return false;
if (ShiftPressed(keyData))
{
code.ExpandSelectionLeftTo(n);
}
else
{
code.SelectionStart = n;
code.SelectionLength = 0;
}
return true;
}
else if (KeyPressed(keyData, Keys.Right) && AltPressed(keyData))
{
if (ShiftPressed(keyData))
{
int n = code.Text.IndexOf('>', code.SelectionEnd() + 1);
if (n < 0) return false;
code.ExpandSelectionRightTo(n + 1);
}
else
{
int n = code.Text.IndexOf('<', code.SelectionStart + 1);
if (n < 0) return false;
code.SelectionStart = n;
code.SelectionLength = 0;
}
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
https://stackoverflow.com/questions/-100007947
复制相似问题