Registering hotkeys in WinForm

Source: Internet
Author: User

Q:winform How do I register a hotkey?

A:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using System.DirectoryServices;
Using System.Runtime.InteropServices; Namespace Test ... {public partial class form1:form ... {Class HotKey ...
            {//If the function succeeds, the return value is not 0. If the function fails to execute, the return value is 0.
            To get extended error messages, call GetLastError.
                [DllImport ("user32.dll", SetLastError = true)] public static extern bool RegisterHotKey (           
                IntPtr hWnd,//the handle int ID of the window to define the hotkey,//define the Hotkey ID (cannot repeat with other IDs)                      Keymodifiers fsmodifiers,//identifies whether the hotkey will take effect when pressing ALT, Ctrl, Shift, windows, etc. keys VK
            Define the contents of the hotkey);
                [DllImport ("user32.dll", SetLastError = true)] public static extern bool Unregisterhotkey ( INtptr hWnd,//the handle int ID of the window to cancel the hotkey//ID to cancel the hotkey);
            Defines the name of a secondary key (converts a number to characters to facilitate memory, but also eliminates this enumeration and uses numeric values directly) [Flags ()] public enum Keymodifiers ...
                {None = 0, Alt = 1, Ctrl = 2, Shift = 4, WindowsKey = 8}} public Form1 () ...
            {InitializeComponent (); Hotkey.registerhotkey (this.
        Handle, 0, KEYS.F1); } protected override void WndProc (ref message m) ...
            {Const int wm_hotkey = 0x0312; Press the shortcut key switch (m.msg) ... {Case Wm_hotkey:switch (M.wparam.toint32 ()) ...
                            {Case 100:messagebox.show ("Press F1");
         Break           } break; Base.
        WndProc (ref m); }     } }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.