Reputation: 3297
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#
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. 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
Upvotes: 2
Views: 23599
Reputation: 3297
For all who have the same problem, I got the solution:
You just have to put KeyboardHook(this, e);
in Form1_Load()
. So the Form1_Load()
should now look like this:
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);
into Form1_Load();
, the program just blocks the key combinations if a key is pressed in webBrowser1
(webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
)
If you put it into Form1_Load();
, it starts blocking combinations immediately.
Of course, this won't work with Win+L
or Ctrl+Alt+Del
because they are hotkeys.
I hope I could help everyone who have the same problem :)
Cheers
Upvotes: 3