简体   繁体   English

使用C#禁用键盘

[英]Disable keyboard using c#

I'm writing a program which is a webbrowser and disable all the keyboard shortcuts and keys. 我正在编写一个程序,它是一个网络浏览器,并禁用了所有键盘快捷键和按键。 It is working but not perfectly. 它正在工作,但并不完美。 i found a thread where i got the code: 我在找到代码的地方找到了一个线程:

Blocking shortcut keys using c# 使用C#阻止快捷键

And my problem is: everytime i open the program, first i have to click in the window or use a shortcut and click in the window again. 我的问题是:每次我打开程序时,首先我必须在窗口中单击或使用快捷方式,然后再次在窗口中单击。 and after that, it works. 然后,它起作用了。 but it have to work when i open it so i don't have to click two times... 但是当我打开它时它必须工作,所以我不必单击两次...

somebody got an idea? 有人知道吗?

cheers 干杯

EDIT: Okay. 编辑:好的。 I try to explain my problem in another way: 我尝试用另一种方式解释我的问题:

First I open the program. 首先,我打开程序。 And then I should not be able to press any keys like Win+Tab etc. But I am still able to press keys. 然后我应该不能按Win + Tab等任何键。但是我仍然可以按键。 Then if I click in the window of my program, press a key and click in the window again, it works. 然后,如果我在程序窗口中单击,请按一个键,然后再次在窗口中单击,它将起作用。 But i want that the program works when I open it so I don't have to click in the window first. 但是我希望该程序在打开时能正常运行,所以我不必先在窗口中单击。 I got some code here: 我在这里有一些代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace Browser
{
    public partial class Form1 : Telerik.WinControls.UI.RadForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        private delegate int LowLevelKeyboardProcDelegate(int nCode, int
           wParam, ref KBDLLHOOKSTRUCT lParam);

        [DllImport("user32.dll", EntryPoint = "SetWindowsHookExA", CharSet = CharSet.Ansi)]
        private static extern int SetWindowsHookEx(
           int idHook,
           LowLevelKeyboardProcDelegate lpfn,
           int hMod,
           int dwThreadId);

        [DllImport("user32.dll")]
        private static extern int UnhookWindowsHookEx(int hHook);

        [DllImport("user32.dll", EntryPoint = "CallNextHookEx", CharSet = CharSet.Ansi)]
        private static extern int CallNextHookEx(
            int hHook, int nCode,
            int wParam, ref KBDLLHOOKSTRUCT lParam);

        const int WH_KEYBOARD_LL = 13;
        private int intLLKey;
        private KBDLLHOOKSTRUCT lParam;

        private struct KBDLLHOOKSTRUCT
        {
            public int vkCode;
            int scanCode;
            public int flags;
            int time;
            int dwExtraInfo;
        }

        private int LowLevelKeyboardProc(
            int nCode, int wParam,
            ref KBDLLHOOKSTRUCT lParam)
        {
            bool blnEat = false;
            switch (wParam)
            {
                case 256:
                case 257:
                case 260:
                case 261:
                    //Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key
                    if (((lParam.vkCode == 9) && (lParam.flags == 32)) ||
                    ((lParam.vkCode == 27) && (lParam.flags == 32)) || ((lParam.vkCode ==
                    27) && (lParam.flags == 0)) || ((lParam.vkCode == 91) && (lParam.flags
                    == 1)) || ((lParam.vkCode == 92) && (lParam.flags == 1)) || ((true) &&
                    (lParam.flags == 32)))
                    {
                        blnEat = true;
                    }
                    break;
            }

            if (blnEat)
                return 1;
            else return CallNextHookEx(0, nCode, wParam, ref lParam);

        }

        private void KeyboardHook(object sender, EventArgs e)
        {
            intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, new LowLevelKeyboardProcDelegate(LowLevelKeyboardProc),
                       System.Runtime.InteropServices.Marshal.GetHINSTANCE(
                       System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]).ToInt32(), 0);
        }

        private void ReleaseKeyboardHook()
        {
            intLLKey = UnhookWindowsHookEx(intLLKey);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

        private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            KeyboardHook(this, e);
        }
    }
}

I hope you know what I mean now Cheers 我希望你知道我现在的意思

For all who have the same problem, I got the solution: 对于所有有相同问题的人,我都有解决方案:

You just have to put KeyboardHook(this, e); 您只需要放入KeyboardHook(this, e); in Form1_Load() . Form1_Load() So the Form1_Load() should now look like this: 因此, Form1_Load()现在应如下所示:

private void Form1_Load(object sender, EventArgs e)
{
    this.WindowState = FormWindowState.Maximized;
    KeyboardHook(this, e);
}

Reason: Everytime the program starts, it should block every combination like Alt+F4 etc. Before we put KeyboardHook(this, e); 原因:每次程序启动时,它都应阻止所有组合,例如Alt + F4等KeyboardHook(this, e); into Form1_Load(); 进入Form1_Load(); , the program just blocks the key combinations if a key is pressed in webBrowser1 ,如果在webBrowser1按下了某个键,该程序只会阻止这些键的组合

( webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) ) webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)

If you put it into Form1_Load(); 如果将其放入Form1_Load(); , it starts blocking combinations immediately. ,它将立即开始阻止组合。

Of course, this won't work with Win+L or Ctrl+Alt+Del because they are hotkeys. 当然,这不适用于Win+LCtrl+Alt+Del因为它们是热键。

I hope I could help everyone who have the same problem :) 希望我能对遇到同样问题的所有人有所帮助:)

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM