我正在编写一个程序,它是一个webbrowser并禁用所有键盘快捷键和键。它工作但不完美。我找到了一个我得到代码的帖子:
Blocking shortcut keys using 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);
}
}
}
我希望你知道我现在的意思 干杯
答案 0 :(得分:1)
对于所有遇到同样问题的人,我得到了解决方案:
您只需将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
中按下某个键,程序就会阻止键组合{1}}
(webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
)
如果您将其放入Form1_Load();
,它会立即开始阻止组合。
当然,这不适用于Win+L
或Ctrl+Alt+Del
,因为它们是热键。
我希望我能帮助每个遇到同样问题的人:)
干杯