如何在C#中使用RegisterHotKey()? [英] how to use RegisterHotKey() in C#?

查看:643
本文介绍了如何在C#中使用RegisterHotKey()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想注册一个热键,我翻译 C ++ code,我写的:

I'm trying register a hot key, I'm translating this C++ code,I wrote it:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("user32.dll")]
        public static extern
            bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, int vk);
        [DllImport("user32")]
        public static extern
            bool GetMessage(ref Message lpMsg, IntPtr handle, uint mMsgFilterInMain, uint mMsgFilterMax);

        public const int MOD_ALT = 0x0001;
        public const int MOD_CONTROL = 0x0002;
        public const int MOD_SHIFT = 0x004;
        public const int MOD_NOREPEAT = 0x400;
        public const int WM_HOTKEY = 0x312;
        public const int DSIX = 0x36;

        static void Main(string[] args)
        {
            if (!RegisterHotKey(IntPtr.Zero, 1, MOD_ALT | MOD_NOREPEAT, DSIX))
            {
                Console.WriteLine("failed key register!");
            }

            Message msg = new Message();

            while (!GetMessage(ref msg, IntPtr.Zero, 0, 0))
            {
                if (msg.message == WM_HOTKEY)
                {
                    Console.WriteLine("do work..");
                }
            }

            Console.ReadLine();
        }
    }

    public class Message
    {
        public int message { get; set; }
    }
}

RegisterHotKey()永远返回false。 我不知道有关方法传递的参数,IntPtr.Zero是是相当于空,消息类,需要在第二个参数对象。有人可以澄清吗?任何帮助是非常AP preciated!

but it RegisterHotKey() ever returns false. I'm not sure about the arguments passed in method, IntPtr.Zero is for be to equivalent to null and message class to be object required in second argument. can someone clarify please? any help is very appreciated!

推荐答案

这可能会帮助:

热键在控制台应用

基本上,你必须创建一个隐藏的形式,使工作

Basically, you have to create a "hidden" form to make it work

这篇关于如何在C#中使用RegisterHotKey()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆