请问在c#中如何实现象象qq中那样接收键盘事件 KeyUp事件中判断if(e.Modifiers == Keys.Control && e.Modifiers == Keys.Alt && e.KeyCode == Keys.z){} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 每个控件都有个AccessKey属性,可以设置它的值,不过它是必须Alt+字母的 同意wangsaokui(无间道III(终极无间)) ( ) 首先,创建一个WinHotKey类,如下public class WinHotKey{ [DllImport("user32.dll",SetLastError=true)] public static extern bool RegisterHotKey( IntPtr hWnd, //窗口句柄 int id, KeyModifiers fsModifiers, Keys vk ); [DllImport("user32.dll",SetLastError=true)] public static extern bool UnregisterHotKey( IntPtr hWnd, int id ); [Flags()] public enum KeyModifiers { None = 0, Alt = 1, Control =2, Shift = 4, Windows = 8 } public WinHotKey() { // // TODO: 在此处添加构造函数逻辑 // }}然后,在程序中这样调用//快捷键定义 private bool key_Ctrl = false; private bool key_Shift = false; private bool key_Alt = false; private bool key_Windows = false; private Keys key_other; public void SetHotKey(bool bCtrl,bool bShift,bool bAlt,bool bWindows,Keys nowKey) { try { this.key_Alt = bAlt; this.key_Ctrl = bCtrl; this.key_Shift = bShift; this.key_Windows = bWindows; this.key_other = nowKey; WinHotKey.KeyModifiers modifier = WinHotKey.KeyModifiers.None; if( this.key_Ctrl ) modifier |= WinHotKey.KeyModifiers.Control; if(this.key_Alt ) modifier |= WinHotKey.KeyModifiers.Alt; if(this.key_Shift) modifier |= WinHotKey.KeyModifiers.Shift; if(this.key_Windows) modifier |= WinHotKey.KeyModifiers.Windows; WinHotKey.RegisterHotKey(Handle,100,modifier,nowKey); } catch { //login.ShowMessage("快捷键定义错误!"); } } //激活热键 protected override void WndProc(ref Message m ) { const int WM_HOTKEY = 0x0312; switch(m.Msg) { case WM_HOTKEY: { //如果有新消息,弹出消息 if( ReceiveNewMessage == true) { for(int i=0;i<this.manInforList.Count;i++) { ManInfor searchMan = (ManInfor)this.manInforList[i]; if( searchMan.manInforID.Equals( getFriendID)) { searchMan.Clicked = true; searchMan.P2PShow(); break; } } } else { this.Show(); this.TopMost = true; this.panel_Main.Refresh(); this.WindowState = System.Windows.Forms.FormWindowState.Normal; } } break; } base.WndProc(ref m ); } QQ用的那是全局热键你在程序中需要自己注册全局热键,用到APIRegisterHotKeyUnregisterHotKey to heroqxn(Brook)能详细讲讲吗 呵呵,那就编出来看看呗。都是高人,学习ing... using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Runtime.InteropServices;namespace HotKey{ public class Form1 : System.Windows.Forms.Form { private bool key_Ctrl = false; private bool key_Shift = false; private bool key_Alt = false; private bool key_Windows = false; private Keys key_other; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Label label1; private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // textBox1 // this.textBox1.BackColor = System.Drawing.SystemColors.HighlightText; this.textBox1.Location = new System.Drawing.Point(8, 32); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.ReadOnly = true; this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.textBox1.Size = new System.Drawing.Size(272, 144); this.textBox1.TabIndex = 0; this.textBox1.Text = ""; // // label1 // this.label1.Location = new System.Drawing.Point(8, 16); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(96, 16); this.label1.TabIndex = 1; this.label1.Text = "Error Message:"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 191); this.Controls.Add(this.label1); this.Controls.Add(this.textBox1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new Form1()); } public void SetHotKey(bool bCtrl,bool bShift,bool bAlt,bool bWindows,Keys nowKey) { try { this.key_Alt = bAlt; this.key_Ctrl = bCtrl; this.key_Shift = bShift; this.key_Windows = bWindows; this.key_other = nowKey; WinHotKey.KeyModifiers modifier = WinHotKey.KeyModifiers.None; if( this.key_Ctrl ) modifier |= WinHotKey.KeyModifiers.Control; if(this.key_Alt ) modifier |= WinHotKey.KeyModifiers.Alt; if(this.key_Shift) modifier |= WinHotKey.KeyModifiers.Shift; if(this.key_Windows) modifier |= WinHotKey.KeyModifiers.Windows; WinHotKey.RegisterHotKey(Handle,100,modifier,nowKey); } catch(Exception err) { this.textBox1.AppendText(err.Message + "\r\n"); } } protected override void WndProc(ref Message m ) { const int WM_HOTKEY = 0x0312; switch(m.Msg) { case WM_HOTKEY: { //如果有新消息 this.textBox1.AppendText(m.Msg.ToString() + "\r\n"); break; } } base.WndProc(ref m ); } private void Form1_Load(object sender, System.EventArgs e) { this.SetHotKey(true, false, false, false, Keys.F10); } } #region WinHotKey Class public class WinHotKey { [DllImport("user32.dll",SetLastError=true)] public static extern bool RegisterHotKey( IntPtr hWnd, //窗口句柄 int id, KeyModifiers fsModifiers, Keys vk ); [DllImport("user32.dll",SetLastError=true)] public static extern bool UnregisterHotKey( IntPtr hWnd, int id ); [Flags()] public enum KeyModifiers { None = 0, Alt = 1, Control =2, Shift = 4, Windows = 8 } public WinHotKey(){} } #endregion} 上边这段程序是好用的 注册的热键是Ctrl + F10, 很简单,我想不用解释也可以明白哦 谢谢heroqxn(Brook) ( ) 大哥 同意wangsaokui(无间道III(终极无间)) heroqxn(Brook)是正确的wangsaokui(无间道III(终极无间))是在窗体有焦点的时候正确的 谁能帮我看一下这个页面的隐藏信息 新装的系统 给个万无一失的安装顺序 紧急求救~~如果使Dialog的父窗口变成活动窗口? 请问能在Visual Studio 2005环境下开发.NET 1.1的程序吗? 大家快帮帮我喔~~~谢谢呢!!!!以下是我发生的问题!! DataSet与xml的操作的问题? 请教为什么很多书籍或示例代码里变量名前面都带一个下划线? 用OleDbDataAdapter如何在程序里动态的写查询,急!!!在线等待 Cookie的一个问题 请教: unicode 显示性别,用什么控件好?
首先,创建一个WinHotKey类,如下
public class WinHotKey
{
[DllImport("user32.dll",SetLastError=true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //窗口句柄
int id,
KeyModifiers fsModifiers,
Keys vk
); [DllImport("user32.dll",SetLastError=true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd,
int id
); [Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control =2,
Shift = 4,
Windows = 8
} public WinHotKey()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}然后,在程序中这样调用
//快捷键定义
private bool key_Ctrl = false;
private bool key_Shift = false;
private bool key_Alt = false;
private bool key_Windows = false;
private Keys key_other; public void SetHotKey(bool bCtrl,bool bShift,bool bAlt,bool bWindows,Keys nowKey)
{
try
{
this.key_Alt = bAlt;
this.key_Ctrl = bCtrl;
this.key_Shift = bShift;
this.key_Windows = bWindows;
this.key_other = nowKey;
WinHotKey.KeyModifiers modifier = WinHotKey.KeyModifiers.None; if( this.key_Ctrl )
modifier |= WinHotKey.KeyModifiers.Control;
if(this.key_Alt )
modifier |= WinHotKey.KeyModifiers.Alt;
if(this.key_Shift)
modifier |= WinHotKey.KeyModifiers.Shift;
if(this.key_Windows)
modifier |= WinHotKey.KeyModifiers.Windows;
WinHotKey.RegisterHotKey(Handle,100,modifier,nowKey);
}
catch
{
//login.ShowMessage("快捷键定义错误!");
}
} //激活热键
protected override void WndProc(ref Message m )
{
const int WM_HOTKEY = 0x0312;
switch(m.Msg)
{
case WM_HOTKEY:
{
//如果有新消息,弹出消息
if( ReceiveNewMessage == true)
{
for(int i=0;i<this.manInforList.Count;i++)
{
ManInfor searchMan = (ManInfor)this.manInforList[i];
if( searchMan.manInforID.Equals( getFriendID))
{
searchMan.Clicked = true;
searchMan.P2PShow();
break;
}
}
}
else
{
this.Show();
this.TopMost = true;
this.panel_Main.Refresh();
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
}
}
break;
}
base.WndProc(ref m );
}
用到API
RegisterHotKey
UnregisterHotKey
能详细讲讲吗
都是高人,学习ing...
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace HotKey
{
public class Form1 : System.Windows.Forms.Form
{
private bool key_Ctrl = false;
private bool key_Shift = false;
private bool key_Alt = false;
private bool key_Windows = false;
private Keys key_other;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
} protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.SystemColors.HighlightText;
this.textBox1.Location = new System.Drawing.Point(8, 32);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(272, 144);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 16);
this.label1.TabIndex = 1;
this.label1.Text = "Error Message:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 191);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion [STAThread]
static void Main()
{
Application.Run(new Form1());
} public void SetHotKey(bool bCtrl,bool bShift,bool bAlt,bool bWindows,Keys nowKey)
{
try
{
this.key_Alt = bAlt;
this.key_Ctrl = bCtrl;
this.key_Shift = bShift;
this.key_Windows = bWindows;
this.key_other = nowKey;
WinHotKey.KeyModifiers modifier = WinHotKey.KeyModifiers.None; if( this.key_Ctrl )
modifier |= WinHotKey.KeyModifiers.Control;
if(this.key_Alt )
modifier |= WinHotKey.KeyModifiers.Alt;
if(this.key_Shift)
modifier |= WinHotKey.KeyModifiers.Shift;
if(this.key_Windows)
modifier |= WinHotKey.KeyModifiers.Windows;
WinHotKey.RegisterHotKey(Handle,100,modifier,nowKey);
}
catch(Exception err)
{
this.textBox1.AppendText(err.Message + "\r\n");
}
} protected override void WndProc(ref Message m )
{
const int WM_HOTKEY = 0x0312;
switch(m.Msg)
{
case WM_HOTKEY:
{
//如果有新消息
this.textBox1.AppendText(m.Msg.ToString() + "\r\n");
break;
}
}
base.WndProc(ref m );
} private void Form1_Load(object sender, System.EventArgs e)
{
this.SetHotKey(true, false, false, false, Keys.F10);
}
}
#region WinHotKey Class
public class WinHotKey
{
[DllImport("user32.dll",SetLastError=true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //窗口句柄
int id,
KeyModifiers fsModifiers,
Keys vk
); [DllImport("user32.dll",SetLastError=true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd,
int id
); [Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control =2,
Shift = 4,
Windows = 8
} public WinHotKey(){}
}
#endregion
}