C# Winform程序 如何让用户自定义键盘热键功能

jxdong1013 2013-06-01 02:43:54
大家好!我的项目中有这样一个需求:用户可以自己定义键盘热键 。
功能和“有道词典”中的“屏幕取词”设置热键一样。


我用键盘钩子去实现这个功能,但一致没有处理好这个功能,求园友帮助。
...全文
499 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
一剑枯荣 2014-07-07
  • 打赏
  • 举报
回复
呵呵,路过,看看
jxdong1013 2013-06-03
  • 打赏
  • 举报
回复
谢谢 caozhy 的帮助!
yanghailun_ 2013-06-02
  • 打赏
  • 举报
回复
引用 2 楼 jxdong1013 的回复:
你这个程序不满足我的需求,我要用户可以自己定义 热键的。 热键不是 程序写死的。
1 楼的代码可以满足你的需求。只是自定义按键你需要自己写判断键盘的按键。 3 楼提供的demo ,完美解决了... 学习了...
threenewbee 2013-06-01
  • 打赏
  • 举报
回复
http://www.codeproject.com/Articles/442285/Global-Shortcuts-in-WinForms-and-WPF
jxdong1013 2013-06-01
  • 打赏
  • 举报
回复
你这个程序不满足我的需求,我要用户可以自己定义 热键的。 热键不是 程序写死的。
tcmakebest 2013-06-01
  • 打赏
  • 举报
回复
热键需要注册使用的:
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("热键消息来了");
        }
    }
}

110,538

社区成员

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

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

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