2

I got a question about KeyCode and disabling special keys. I know this question was asked a few times, but I didn't find an answer I can use and which works so I came here to ask :)

I'm writing a program which blocks every key or key combinations (like Alt+F4 etc.). The application is not for me, it's for customers which only be able to navigate in this program. This all works fine, but I can't disable Left CTRL, Right CTRL or Alt key. I got this code for try blocking these keys:

private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.LControlKey)
            {
                MessageBox.Show("LCtrl", "Warnung", MessageBoxButtons.OK);
            }

            else if (e.KeyCode == Keys.RControlKey)
            {
                MessageBox.Show("RCtrl", "Warnung", MessageBoxButtons.OK);

            }

            else if (e.KeyCode == Keys.Alt)
            {
                MessageBox.Show("Alt", "Warnung", MessageBoxButtons.OK);
            }

            else if (e.KeyCode == Keys.Delete)
            {
                MessageBox.Show("Delete", "Warnung", MessageBoxButtons.OK);
            }
        }

I only use MessageBox.Show(); that I can see if it works. Delete key works fine, but the other one not. Is it possible to do this without editing the registry and for Win7? Does anyone know why or can give me a hint?

Cheers

EDIT: I block all other keys in this way: Blocking shortcut keys using c#

4

3 回答 3

4

免责声明:我在用户输入类方面经验并不丰富,但这是我的输入。

CTRL 和 ALT 是修饰键的示例。也就是说,他们修改其他(非修饰符)键以创建组合键。您的 UI 可能只能选择完整的组合键。例如:

private void keyPressed(object sender, PreviewKeyDownEventArgs e)
{
    e.KeyCode == Key.A; // True (pressed A)
    e.KeyCode == Key.Control; // False (no key pressed)
    e.Modifiers == Keys.Control; // True (user is pressing the modifier CTRL)
    e.KeyCode == Key.A && e.Modifiers == Keys.Control; (pressed key A with modifier CTRL)
}

至于禁用密钥,您可以捕获 e.Modifiers:

private void ignoreCtrl(object sender, PreviewKeyDownArgs e)
{
    if (e.Modifiers != Keys.Control) { /* Pass to handler */ }
    else { /* Discard */ }
}

同样,我对您的特定框架没有经验,但这是我的猜测。我使用了以下 SO 来源:

如何在 C# 中使用多个修饰键

确定是否按下了修饰键

于 2013-09-19T08:16:56.853 回答
1

这应该是一个注释,因为我没有测试它,但我需要一些代码作为示例,所以我写在这里。你告诉我它是否有效。

PreviewKeyDownEventArgs包含可用于检查是否按下了一个或多个修饰键的其他属性。
然后,您可以尝试将IsInputKey属性设置为 false 以防止对任何常规键与修饰键一起按下的进一步处理。

private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)

if (e.Control)
{
    MessageBox.Show("Ctrl", "Warnung", MessageBoxButtons.OK);
    e.IsInputKey = false;
}
else if (e.Alt)
{
    MessageBox.Show("Alt", "Warnung", MessageBoxButtons.OK);
    e.IsInputKey = false;
}
于 2013-09-19T08:09:32.047 回答
1

您是否尝试过检查修饰键?比如下面。

请注意,这是针对 WPF 的,因为您没有说明 win 表格或 wpf。

if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
    MessageBox.Show("Control Button Down");
}
else if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
{
    MessageBox.Show("Alt Down");
}

干杯。

于 2013-09-19T08:10:29.360 回答