i want to disable the hotkeys such as CTRL, ALT, ESC, ALT+F4,........
i tried this code..but i get some number like 1400,1409 as the error message.
i dont know how to solve this error...
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Collections;
using
System.ComponentModel;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.Runtime.InteropServices;
using
System.Diagnostics;
///
<summary>
///
Summary description for ki
///
</summary>
public partial
class ki:Form
{
#region dll
[
DllImport("user32.dll")]
private static
extern int RegisterHotKey(IntPtr hwnd,
int id, int fsModifiers,
int vk);
[
DllImport("user32.dll")]
private static
extern int ShowWindow(int hwnd,
int cmd);
[
DllImport("user32.dll")]
private static
extern int FindWindow(string cls,
string wndwText);
[DllImport("user32.dll")]
private static
extern int UnregisterHotKey(IntPtr hwnd,
int id);
#endregion
#region con
private const
int USE_ALT = 1;
private const
int USE_CTRL = 2;
short mHotKeyId = 0;
#endregion
public ki()
{
//
// TODO: Add constructor logic here
//
//InitializeComponent();
RegisterGlobalHotKey(Keys.F4, USE_ALT); <--------- i got the error message while executing this line
RegisterGlobalHotKey(
Keys.W, USE_CTRL);
// Disable ALT+Tab - tab through open applications
RegisterGlobalHotKey(Keys.Tab, USE_ALT);
// hide the task bar - not a big deal, they can
// still CTRL+ESC to get the start menu; for that
// matter, CTRL+ALT+DEL also works; if you need to
// disable that you will have to violate SAS and
// monkey with the security policies on the machine
//ShowWindow(FindWindow("Shell_TrayWnd", null), 0);
}
private void RegisterGlobalHotKey(Keys hotkey,
int modifiers)
{
try
{
// increment the hot key value - we are just identifying
// them with a sequential number since we have multiples
mHotKeyId++;
if(mHotKeyId > 0)
{
// register the hot key combination
if (RegisterHotKey(this.Handle, mHotKeyId, modifiers,
Convert.ToInt16(hotkey)) == 0)
{
// tell the user which combination failed to register
// this is useful to you, not an end user; the user
// should never see this application run
MessageBox.Show("Error: " + mHotKeyId.ToString() +
" - " +Marshal.GetLastWin32Error().ToString(),"HotKey Registration");
}
}
}
catch
{
// clean up if hotkey registration failed -
// nothing works if it fails
UnregisterGlobalHotKey();
}
}
public void UnregisterGlobalHotKey()
{
// loop through each hotkey id and
// disable it
for (int i = 0; i < mHotKeyId; i++)
{
UnregisterHotKey(this.Handle, i);
}
}
protected override
void WndProc(ref
Message m)
{
base.WndProc(ref m);
// if the message matches,
// disregard it
const
int WM_HOTKEY = 0x312;if (m.Msg == WM_HOTKEY)
{
// Ignore the request or each
// disabled hotkey combination
nithra08
Member
1 Points
75 Posts
Disable hot keys using c#
Mar 28, 2008 01:52 PM|LINK
hi,
i'm doing my application in asp.net with c#.
i want to disable the hotkeys such as CTRL, ALT, ESC, ALT+F4,........
i tried this code..but i get some number like 1400,1409 as the error message.
i dont know how to solve this error...
using
System;using
System.Data;using
System.Configuration;using
System.Web;using
System.Web.Security;using
System.Web.UI;using
System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts;using
System.Web.UI.HtmlControls;using
System.Collections;using
System.ComponentModel;using
System.Drawing;using
System.Text;using
System.Windows.Forms;using
System.Runtime.InteropServices;using
System.Diagnostics; /// <summary>///
Summary description for ki///
</summary> public partial class ki:Form{
#region dll[
DllImport("user32.dll")] private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk);[
DllImport("user32.dll")] private static extern int ShowWindow(int hwnd, int cmd);[
DllImport("user32.dll")] private static extern int FindWindow(string cls, string wndwText);
[DllImport("user32.dll")] private static extern int UnregisterHotKey(IntPtr hwnd, int id);#endregion
#region con private const int USE_ALT = 1; private const int USE_CTRL = 2; short mHotKeyId = 0;#endregion
public ki(){
// // TODO: Add constructor logic here // //InitializeComponent();
RegisterGlobalHotKey(Keys.F4, USE_ALT); <--------- i got the error message while executing this lineRegisterGlobalHotKey(
Keys.W, USE_CTRL); // Disable ALT+Tab - tab through open applications RegisterGlobalHotKey(Keys.Tab, USE_ALT); // hide the task bar - not a big deal, they can // still CTRL+ESC to get the start menu; for that // matter, CTRL+ALT+DEL also works; if you need to // disable that you will have to violate SAS and // monkey with the security policies on the machine //ShowWindow(FindWindow("Shell_TrayWnd", null), 0);}
private void RegisterGlobalHotKey(Keys hotkey, int modifiers){
try{
// increment the hot key value - we are just identifying // them with a sequential number since we have multiplesmHotKeyId++;
if(mHotKeyId > 0){
// register the hot key combination if (RegisterHotKey(this.Handle, mHotKeyId, modifiers, Convert.ToInt16(hotkey)) == 0){
// tell the user which combination failed to register // this is useful to you, not an end user; the user // should never see this application run MessageBox.Show("Error: " + mHotKeyId.ToString() + " - " +Marshal.GetLastWin32Error().ToString(),"HotKey Registration");}
}
}
catch{
// clean up if hotkey registration failed - // nothing works if it failsUnregisterGlobalHotKey();
}
}
public void UnregisterGlobalHotKey(){
// loop through each hotkey id and // disable it for (int i = 0; i < mHotKeyId; i++){
UnregisterHotKey(this.Handle, i);}
}
protected override void WndProc(ref Message m){
base.WndProc(ref m); // if the message matches, // disregard it const int WM_HOTKEY = 0x312;if (m.Msg == WM_HOTKEY){
// Ignore the request or each // disabled hotkey combination}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e){
UnregisterGlobalHotKey();
// show the taskbar - does not matter really ShowWindow(FindWindow("Shell_TrayWnd", null), 1);}
}
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Disable hot keys using c#
Mar 28, 2008 02:10 PM|LINK
Is this a Windows client app? If so, you are better off posting here: http://windowsclient.net/. It uses the same login as forums.asp.net, by the way.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter