2

我正在编写一个程序,它是一个网络浏览器并禁用所有键盘快捷键和键。它正在工作,但并不完美。我找到了一个我得到代码的线程:

使用 c# 阻止快捷键

我的问题是:每次打开程序时,首先我必须单击窗口或使用快捷方式并再次单击窗口。之后,它就可以工作了。但是当我打开它时它必须工作,所以我不必点击两次......

有人有想法吗?

干杯

编辑:好的。我尝试用另一种方式解释我的问题:

首先我打开程序。然后我应该不能按 Win+Tab 等任何键。但我仍然可以按键。然后,如果我在我的程序窗口中单击,按一个键并再次单击窗口,它就可以工作了。但是我希望该程序在我打开它时可以工作,所以我不必先在窗口中单击。我在这里得到了一些代码:

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);
        }
    }
}

我希望你现在明白我的意思 干杯

4

1 回答 1

2

对于所有有同样问题的人,我得到了解决方案:

你只需要KeyboardHook(this, e);输入Form1_Load(). 所以Form1_Load()现在应该是这样的:

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

原因:每次程序启动时,它应该阻止每个组合,如 Alt+F4 等。在我们KeyboardHook(this, e);输入之前Form1_Load();,如果按下某个键,程序只会阻止组合键webBrowser1

( webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e))

如果您将其放入Form1_Load();,它会立即开始阻止组合。

当然,这不适用于Win+LCtrl+Alt+Del因为它们是热键。

我希望我能帮助所有有同样问题的人:)

干杯

于 2013-09-20T20:21:46.863 回答