Show / Hide Table of Contents

MSDN.WhiteKnight - Stack Overflow answers

Ответ на "работа горячих клавиш программы в свернутом режиме c#"

Answer 901192

Link

Используйте функцию RegisterHotKey:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

        //необходимые константы
        public const int MOD_ALT = 0x1;
        public const int MOD_CONTROL = 0x2;
        public const int MOD_SHIFT = 0x4;
        public const int MOD_WIN = 0x8;
        public const int WM_HOTKEY = 0x312;        

        //обработчик сообщений для окна
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_HOTKEY)
            {
                textBox1.Text += DateTime.Now.ToString() + ". Hotkey pressed, ID = 0x" + m.WParam.ToString("X");
                textBox1.Text += Environment.NewLine;
                m.Result = (IntPtr)0;
                return;
            }

            base.WndProc(ref m);
        }


        public Form1()
        {
            InitializeComponent();

            bool res = RegisterHotKey(this.Handle, 1, 0, (uint)Keys.F1);//регистрируем горячую клавишу
            if (res == false) MessageBox.Show("RegisterHotKey failed");
        }
    }
}

Content is retrieved from StackExchange API.

Auto-generated by ruso-archive tools.

Back to top Stack Overflow answers (published from sources in GitHub repository). Copyright (c) 2020, MSDN.WhiteKnight. Content licensed under BSD 3-Clause License.
Generated by DocFX