获取在WPF窗口中输入的长组合密钥(密码)

时间:2017-02-15 08:04:44

标签: c# wpf keyboard-shortcuts keydown

我对这里的帖子感到有些困惑。如何获得5-10键的组合键?我打算捕获"!qaz $ esz"没有双引号的密钥。

我的应用程序是一个全屏应用程序,在输入正确的密钥以启用桌面访问之前阻止访问桌面。我杀了explorer.exe并在程序启动时禁用任务管理器,并在组合密码正确后启用它: -

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        /** 
         * 1.   KILL EXPLORER
         * 2.   DISABLE KEY TO TERMINATE
         *      = Alt + F4
         *      = Win + Tab
         *      = Win + D
         *      = etc
         * 3.   ACCEPT SPECIFIC KEY ONLY TO UNLOCK TO MINIMIZE AND SET TOP = FALSE
         * 
         * 
         * **/
        RegistryKey regkey = SetKey(TaskManager.Disabled);
        regkey.Close();
    }
    public static RegistryKey SetKey(TaskManager command)
    {
        RegistryKey mKey;
        string subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
        mKey = Registry.CurrentUser.CreateSubKey(subKey);
        switch (command)
        {
            case TaskManager.Enabled:
                mKey.SetValue("DisableTaskMgr", 0);
                break;
            case TaskManager.Disabled:
                mKey.SetValue("DisableTaskMgr", 1);
                break;
        }
        return mKey;
    }

但是我的代码必须缺少一些东西,我无法检测到更多密码的组合代码。

以下代码是我测试以查看我输入的密钥是否被捕获的信息。

 public MainWindow()
    {
        InitializeComponent();
        AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)HandleKeyDownEvent);
    }
    private void HandleKeyDownEvent(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Tab && (Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Shift)) == (ModifierKeys.Control | ModifierKeys.Shift))
        {
            MessageBox.Show("CTRL + SHIFT + TAB trapped"); // Working
        }

        if (e.Key == Key.Tab && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
        {
            MessageBox.Show("CTRL + TAB trapped"); // Working
        }

        if (e.Key == Key.F4 && Keyboard.Modifiers.HasFlag(ModifierKeys.Alt))
        {
            MessageBox.Show("ALT + F4 trapped"); // Not working
        }
        if (e.Key == Key.D && Keyboard.Modifiers.HasFlag(ModifierKeys.Windows))
        {
            MessageBox.Show("WIN + D trapped"); // Not working
        }
        if ((Keyboard.IsKeyDown(Key.LeftShift) && e.Key == Key.D1) && e.Key ==Key.Q && e.Key == Key.A && e.Key == Key.Z && e.Key == Key.D4 && e.Key == Key.E && e.Key == Key.S && e.Key == Key.Z)
        {
            MessageBox.Show("COMBOKEY"); // Not working
            // RUN EXPLORER
            // ENABLE TASK MANAGER
        }
    }

什么不解决的是 WINLOGO + D ALT + TAB ALT + F4 和LONG COMBINATION KEY。

我从这里尝试了一些代码: -

  1. https://stackoverflow.com/a/5750757/3436326
  2. https://stackoverflow.com/a/17088823/3436326
  3. 我看到有关写入LowLevelAPI的讨论/发布/文章。我不熟悉访问低级API并将其应用于WPF应用程序: -

    1. http://www.tamas.io/c-disable-ctrl-alt-del-alt-tab-alt-f4-start-menu-and-so-on/
    2. https://www.codeproject.com/articles/14485/low-level-windows-api-hooks-from-c-to-stop-unwante

1 个答案:

答案 0 :(得分:1)

e.Key == Key.Q && e.Key == Key.A

如果e.Key等于Key.Q,则不能同时等于Key.A。这就是为什么它不起作用。您要检测的是这些键的序列,并且由于您只有一个键监听器,您必须自己管理状态:

private int passwordState = 0;

private void HandleKeyDownEvent(object sender, KeyEventArgs e)
{
    // other checks

    else if (Keyboard.IsKeyDown(Key.LeftShift) && e.Key == Key.D1)
        passwordState = 1; // first key of the password
    else if (passwordState == 1 && e.Key == Key.Q)
        passwordState = 2;
    else if (passwordState == 2 && e.Key == Key.A)
        passwordState = 3;
    else if (passwordState == 3 && e.Key == Key.Z)
        passwordState = 4;
    else if (passwordState == 4 && e.Key == Key.D4)
        passwordState = 5;
    else if (passwordState == 5 && e.Key == Key.E)
        passwordState = 6;
    else if (passwordState == 6 && e.Key == Key.S)
        passwordState = 7;
    else if (passwordState == 7 && e.Key == Key.Z)
    {
        passwordState = 0;

        // correct password
        MessageBox.Show("COMBOKEY");
    }
    else
        passwordState = 0; // some other/unexpected key: reset password state
}

但这对维护来说真的很烦人,那么为什么你没有一个特殊的组合键而只是简单地显示用户输入密码的模态提示?是的,您会向用户显示一个明确的密码提示,然后他们可能会意外发现,但问题是什么?