我正在编写一个程序,它是一个网络浏览器并禁用所有键盘快捷键和键。它正在工作,但并不完美。我找到了一个我得到代码的线程:
我的问题是:每次打开程序时,首先我必须单击窗口或使用快捷方式并再次单击窗口。之后,它就可以工作了。但是当我打开它时它必须工作,所以我不必点击两次......
有人有想法吗?
干杯
编辑:好的。我尝试用另一种方式解释我的问题:
首先我打开程序。然后我应该不能按 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);
}
}
}
我希望你现在明白我的意思 干杯