Mở phần mềm bằng phím nóng trong c#

Mở phần mềm bằng phím nóng trong c#

using System.Runtime.InteropServices;

 

[DllImport("user32.dll")]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);

 

Sau cái

 InitializeComponent();

 int HotKeyCode = (int)Keys.F9;
            // Register the "F9" hotkey
            Boolean F9Registered = RegisterHotKey(
                this.Handle, UniqueHotkeyId, 0x0000, HotKeyCode
            );

 

protected override void WndProc(ref Message m)
        {
            // 5. Catch when a HotKey is pressed !
            if (m.Msg == 0x0312)
            {
                int id = m.WParam.ToInt32();
                // MessageBox.Show(string.Format("Hotkey #{0} pressed", id));

                if (id == 0)
                {
                    this.Show();
                    this.WindowState = FormWindowState.Normal;
                    
                    
                }
            }

            base.WndProc(ref m);
        }

Để form mở, sau đó ẩn đi

 

Tham khảo:

https://stackoverflow.com/questions/2450373/set-global-hotkeys-using-c-sharp

 https://laptrinhvb.net/bai-viet/devexpress/---Csharp----Huong-dan-tao-hotkey-cho-ung-dung-winform/61a1ae9cd3c11d33.html

https://ourcodeworld.com/articles/read/573/how-to-register-a-single-or-multiple-global-hotkeys-for-a-single-key-in-winforms

Mở phần mềm bằng phím nóng trong c#