C# Winform程序 如何让用户自定义键盘热键功能 c#Winform键盘热键 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 热键需要注册使用的:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;using System.Windows.Forms;namespace WindowsFormsApplication1{ class Hotkey { /// <summary> /// 快捷键消息 /// </summary> public const int WM_HOTKEY = 0x0312; [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 ); [DllImport("kernel32.dll", EntryPoint = "GlobalAddAtomW")] private static extern int GlobalAddAtomW(string name); [Flags()] public enum KeyModifiers { None = 0, Alt = 1, Control = 2, Shift = 4, Windows = 8 } public static int GlobalAddAtom(string name) { return GlobalAddAtomW(name) - 0xC000; } }}bool hotok1 = false;int hotid1 = Hotkey.GlobalAddAtom("myhotkey1");private void Form1_Load(object sender, EventArgs e){ hotok1 = Hotkey.RegisterHotKey(this.Handle, hotid1, Hotkey.KeyModifiers.None, Keys.F2);}private void Form1_FormClosed(object sender, FormClosedEventArgs e){ if (hotok1) { Hotkey.UnregisterHotKey(this.Handle, hotid1); }}protected override void WndProc(ref Message m){ base.WndProc(ref m); if (m.Msg == Hotkey.WM_HOTKEY) { int id = m.WParam.ToInt32(); if (id == hotid1) { MessageBox.Show("热键消息来了"); } }} http://www.codeproject.com/Articles/442285/Global-Shortcuts-in-WinForms-and-WPF 1 楼的代码可以满足你的需求。只是自定义按键你需要自己写判断键盘的按键。3 楼提供的demo ,完美解决了...学习了... C# 求解 很简单的一个代码 本人刚刚接触 WPF的Window_Loaded问题 怎么得到曲线通过哪些像素点位? (C# 语音识别) 读文本内容的时候,为什么不能读到英文的时候,就出现异常----一直在读乱七八糟的英文,请教高手. 请教这两个语句的用法 MouseDown问题 一简单问题 子窗体,父窗体传值问题 C#.NET 上传图片时怎样限制文件格式? 问一个winform的DataGrid的问题 WPF关于动画在不同的硬件上的性能问题 System.NotSupportedException: 不支持给定路径的格式。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
class Hotkey
{
/// <summary>
/// 快捷键消息
/// </summary>
public const int WM_HOTKEY = 0x0312; [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 ); [DllImport("kernel32.dll", EntryPoint = "GlobalAddAtomW")]
private static extern int GlobalAddAtomW(string name); [Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
} public static int GlobalAddAtom(string name)
{
return GlobalAddAtomW(name) - 0xC000;
}
}
}
bool hotok1 = false;
int hotid1 = Hotkey.GlobalAddAtom("myhotkey1");private void Form1_Load(object sender, EventArgs e)
{
hotok1 = Hotkey.RegisterHotKey(this.Handle, hotid1, Hotkey.KeyModifiers.None, Keys.F2);
}private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (hotok1)
{
Hotkey.UnregisterHotKey(this.Handle, hotid1);
}
}protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == Hotkey.WM_HOTKEY)
{
int id = m.WParam.ToInt32();
if (id == hotid1)
{
MessageBox.Show("热键消息来了");
}
}
}
3 楼提供的demo ,完美解决了...
学习了...