c#在全局热键上添加延迟

问题描述 投票:0回答:1

我有全局热键,并且我试图增加延迟,但我使用了lastPressedTime.AddSeconds(.5),但没有成功。

我不像以前那样使用表单事件。

private void Menu_Load(object sender, EventArgs e)
        {
            textBox1.ReadOnly = true;
            IntPtr thisWindow = this.Handle;
            RegisterHotKey(thisWindow, 1, (uint)fsModifiers.Control, (uint)Keys.Q);
            RegisterHotKey(thisWindow, 2, (uint)fsModifiers.Control, (uint)Keys.W);
            RegisterHotKey(thisWindow, 3, (uint)fsModifiers.Control, (uint)Keys.E);
        }

        public enum fsModifiers
        {
            NoModifier=0x000,
            Alt = 0x0001,
            Control = 0x0002,
            Shift = 0x0004,
            Window = 0x0008,
        }

        private void Menu_FormClosed(object sender, FormClosedEventArgs e)
        {
            UnregisterHotKey(thisWindow, 1);
        }

        protected override void WndProc(ref Message keyPressed)
        {
            IntPtr i = keyPressed.WParam;
            DateTime lastPressedTime = DateTime.MinValue;

            if (DateTime.Now > lastPressedTime.AddSeconds(.5)&& i == (IntPtr)1 && keyPressed.Msg == 0x0312)
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    ShowWindow(this.Handle, SW_RESTORE);
                    TopMost = true;
                }
                else
                {
                    this.WindowState = FormWindowState.Minimized;
                    TopMost = false;
                }
            }

谢谢

c# global keypress hotkeys
1个回答
0
投票

此行

DateTime lastPressedTime = DateTime.MinValue;

您只是在if语句中向DateTime.MinValue添加.5秒,所以DateTime.Now始终大于该值。您需要将lastPressedTime设置为按下键的时间(它可能已经在您的消息对象?)。

© www.soinside.com 2019 - 2024. All rights reserved.