gpt4 book ai didi

c# - 如何在 C# 中使用 RegisterHotKey()?

转载 作者:太空狗 更新时间:2023-10-29 17:59:39 24 4
gpt4 key购买 nike

<分区>

我正在尝试注册一个热键,我正在翻译 this C++ 代码转换为 C#:

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 应该为 null,消息类构造函数的第二个参数需要一个对象。非常感谢任何帮助!

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com