我想禁掉系统快捷键,因为代码是另一个网友给的,我对hook也
不太了解,求高人帮忙。
禁掉如:Alt + Tab, Ctrl + Esc, Ctrl + Alt + Del, Ctrl + Shift + Esc, 左右Win键等等一些系统快捷键。
本意是想仿照Windows的屏幕保护,退出屏保时只有输入正确密码才能退出....
例子代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace 钩子
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
SetHook();
} internal enum HookType
{
//msgfilter = -1,
//journalrecord = 0,
//journalplayback = 1, Keyboard = 2, //getmessage = 3,
//callwndproc = 4,
//cbt = 5,
//sysmsgfilter = 6,
//mouse = 7,
//hardware = 8,
//debug = 9,
//shell = 10,
//foregroundidle = 11,
//callwndprocret = 12,
//keyboardll = 13,
}; IntPtr _nextHookPtr; //记录hook编号
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId(); //取得当前线程编号的API [DllImport("user32.dll")]
internal extern static void UnhookWindowsHookEx(IntPtr handle); //取消hook的api [DllImport("user32.dll")]
internal extern static IntPtr SetWindowsHookEx(int idHook, [MarshalAs(UnmanagedType.FunctionPtr)] HookProc lpfn, IntPtr hinstance, int threadID); //设置hook的api [DllImport("user32.dll")]
internal extern static IntPtr CallNextHookEx(IntPtr handle, int code, IntPtr wparam, IntPtr lparam); internal delegate IntPtr HookProc(int code, IntPtr wparam, IntPtr lparam);
IntPtr MyHookProc(int code, IntPtr wparam, IntPtr lparam)
{
if (code < 0)
return CallNextHookEx(_nextHookPtr, code, wparam, lparam);
if (wparam.ToInt32() == 98 && wparam.ToInt32() == 66) //这里就是不知道怎么禁掉Ctrl等键
{
this.textBox1.Text = "a";
return (IntPtr)1;
}
else
{
return IntPtr.Zero;
}
} public void SetHook()
{
if (_nextHookPtr != IntPtr.Zero)
return; HookProc myhookProc = new HookProc(MyHookProc);
_nextHookPtr = SetWindowsHookEx((int)HookType.Keyboard,myhookProc,IntPtr.Zero,GetCurrentThreadId());
}
public void UnHook()
{
if (_nextHookPtr != IntPtr.Zero)
{
UnhookWindowsHookEx(_nextHookPtr);
_nextHookPtr = IntPtr.Zero;
}
} private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
UnHook();
}
}
}
不太了解,求高人帮忙。
禁掉如:Alt + Tab, Ctrl + Esc, Ctrl + Alt + Del, Ctrl + Shift + Esc, 左右Win键等等一些系统快捷键。
本意是想仿照Windows的屏幕保护,退出屏保时只有输入正确密码才能退出....
例子代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace 钩子
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
SetHook();
} internal enum HookType
{
//msgfilter = -1,
//journalrecord = 0,
//journalplayback = 1, Keyboard = 2, //getmessage = 3,
//callwndproc = 4,
//cbt = 5,
//sysmsgfilter = 6,
//mouse = 7,
//hardware = 8,
//debug = 9,
//shell = 10,
//foregroundidle = 11,
//callwndprocret = 12,
//keyboardll = 13,
}; IntPtr _nextHookPtr; //记录hook编号
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId(); //取得当前线程编号的API [DllImport("user32.dll")]
internal extern static void UnhookWindowsHookEx(IntPtr handle); //取消hook的api [DllImport("user32.dll")]
internal extern static IntPtr SetWindowsHookEx(int idHook, [MarshalAs(UnmanagedType.FunctionPtr)] HookProc lpfn, IntPtr hinstance, int threadID); //设置hook的api [DllImport("user32.dll")]
internal extern static IntPtr CallNextHookEx(IntPtr handle, int code, IntPtr wparam, IntPtr lparam); internal delegate IntPtr HookProc(int code, IntPtr wparam, IntPtr lparam);
IntPtr MyHookProc(int code, IntPtr wparam, IntPtr lparam)
{
if (code < 0)
return CallNextHookEx(_nextHookPtr, code, wparam, lparam);
if (wparam.ToInt32() == 98 && wparam.ToInt32() == 66) //这里就是不知道怎么禁掉Ctrl等键
{
this.textBox1.Text = "a";
return (IntPtr)1;
}
else
{
return IntPtr.Zero;
}
} public void SetHook()
{
if (_nextHookPtr != IntPtr.Zero)
return; HookProc myhookProc = new HookProc(MyHookProc);
_nextHookPtr = SetWindowsHookEx((int)HookType.Keyboard,myhookProc,IntPtr.Zero,GetCurrentThreadId());
}
public void UnHook()
{
if (_nextHookPtr != IntPtr.Zero)
{
UnhookWindowsHookEx(_nextHookPtr);
_nextHookPtr = IntPtr.Zero;
}
} private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
UnHook();
}
}
}
你看看这个帖子里面我回复的内容,整理一下就是你要的东西
但是怎么禁掉Ctrl + Tab等键就不会了。
求高人,
这问题困扰我好久了...
我不太明白这个,能否给个完整的代码。
我邮箱[email protected]
谢谢了~
像下面这样使用
public partial class FormMain : Form
{
public FormMain()
{
actHook = new Cls.UserActivityHook();
actHook.OnMouseActivity += new MouseEventHandler(MouseMoved);
actHook.KeyDown += new KeyEventHandler(MyKeyDown);
actHook.KeyPress += new KeyPressEventHandler(MyKeyPress);
actHook.KeyUp += new KeyEventHandler(MyKeyUp);
actHook.Start();
}
private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
{
actHook.Stop();
}
#region golbal hook
Cls.UserActivityHook actHook;
public void MouseMoved(object sender, MouseEventArgs e)
{
//
}
public void MyKeyDown(object sender, KeyEventArgs e)
{
//
} public void MyKeyPress(object sender, KeyPressEventArgs e)
{
//
} public void MyKeyUp(object sender, KeyEventArgs e)
{
//
}
#endregion
}
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
...
}
这个代码是自己写的,什么时候调用,怎么返回,返回什么?
不好意思,好多不懂的...
public void Start(bool InstallMouseHook, bool InstallKeyboardHook)
{
if (hMouseHook == 0)
{
//Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.
int errorCode = Marshal.GetLastWin32Error();
//do cleanup
Stop(true, false, false);
//Initializes and throws a new instance of the Win32Exception class with the specified error.
throw new Win32Exception(errorCode);//这里包错,说操作成功完成
}
... if (hKeyboardHook == 0)
{
//Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.
int errorCode = Marshal.GetLastWin32Error();
//do cleanup
Stop(false, true, false);
//Initializes and throws a new instance of the Win32Exception class with the specified error.
throw new Win32Exception(errorCode);//这里包错,说操作成功完成
}
}
两段代码注释掉后,鼠标点击开始工具栏时是被禁掉了,
但是键盘还是可以用