请问在哪里写的键盘事件可以实现无论焦点在窗口哪个控件上都可以触发?

marty2000 2004-11-21 10:05:28
我自己用picturebox做成按钮,但是picturebox不能像button控件那样可以在按钮上的文字设置下划线快捷键,如果我要实现这个功能该怎么做?
...全文
206 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
小蕊_claudia 2004-11-23
  • 打赏
  • 举报
回复
学习~
fireyan 2004-11-23
  • 打赏
  • 举报
回复
钩子,自己去找吧
amami 2004-11-23
  • 打赏
  • 举报
回复
UP
marty2000 2004-11-23
  • 打赏
  • 举报
回复
up
哈哈007哈 2004-11-22
  • 打赏
  • 举报
回复
sc
marty2000 2004-11-22
  • 打赏
  • 举报
回复
up
cnhgj 2004-11-22
  • 打赏
  • 举报
回复
按Alt+S就会弹出对话框了..
marty2000 2004-11-22
  • 打赏
  • 举报
回复
能给个例子吗?
cnhgj 2004-11-22
  • 打赏
  • 举报
回复
private void Form1_Load(object sender, System.EventArgs e)
{
this.KeyPreview = true;
}

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.S)
{
pictureBox1ClickEvent();
}
}

void pictureBox1ClickEvent()
{
MessageBox.Show("test");
}

private void pictureBox1_Click(object sender, System.EventArgs e)
{
pictureBox1ClickEvent();
}

大概这样.
marty2000 2004-11-22
  • 打赏
  • 举报
回复
为什么我在Form1_Load中加了多个HOTKEY
this.SetHotKey(true, false, false, false, Keys.A);
this.SetHotKey(true, false, false, false, Keys.B);
但是只有一个起作用呢?

还有就是,如果这样用的话不是把我所有的键盘都屏蔽了吗?那我还要输入其他东西怎么办?我只是像增加多几个键盘事件罢了。
hatita 2004-11-22
  • 打赏
  • 举报
回复

焦点在PictureBox1,使用Enter触发,去掉ActiveControl.Name == "PictureBox1"使用Enter就触发
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if(ActiveControl.Name == "PictureBox1" && keyData == Keys.Enter)
{
//操作代码
}

return base.ProcessCmdKey (ref msg, keyData);
}



北京的雾霾天 2004-11-22
  • 打赏
  • 举报
回复
http://blog.csdn.net/hbxtlhx/archive/2004/11/11/177350.aspx
CMIC 2004-11-22
  • 打赏
  • 举报
回复
全局热键:
首先,创建一个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 );
}
----------------------------------------------------------------------------------------------
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
}
xiaoslong 2004-11-22
  • 打赏
  • 举报
回复
帮你顶
Question159635757 2004-11-22
  • 打赏
  • 举报
回复
TO:hbxtlhx(下着春雨的天)
老大,理论上这么说,给点代码意思意思呀
tongcheng 2004-11-22
  • 打赏
  • 举报
回复
看看。。。
北京的雾霾天 2004-11-22
  • 打赏
  • 举报
回复
全局hook的就是了.也就是写一个键盘钩子程序.
cxyPioneer 2004-11-21
  • 打赏
  • 举报
回复
up

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧