C # implement the system hotkey Function

Source: Internet
Author: User

Use windows api to implement the system hotkey function!
The main categories are designed as follows:
Using System;
Using System. Windows. Forms; // for Key namespace
Using System. Runtime. InteropServices;
 
System hotkeys implemented by namespace
{
/// <Summary>
/// WIN32 Windows API-assisted operation class.
/// </Summary>
Public class NativeWIN32
{
Public NativeWIN32 ()
{}
/* ------- Using WIN32 Windows API in a C # application -------*/
 
[DllImport ("user32.dll", CharSet = CharSet. Auto)]
Static public extern IntPtr GetForegroundWindow ();//
 
[StructLayout (LayoutKind. Sequential, CharSet = CharSet. Auto)]
Public struct STRINGBUFFER
{
[Financialas (UnmanagedType. ByValTStr, SizeConst = 256)]
Public string szText;
}
 
[DllImport ("user32.dll", CharSet = CharSet. Auto)]
Public static extern int GetWindowText (IntPtr hWnd, out STRINGBUFFER ClassName, int nMaxCount );
 
[DllImport ("user32.dll", CharSet = CharSet. Auto)]
Public static extern int SendMessage (IntPtr hWnd, int msg, int wParam, int lParam );
 
[DllImport ("user32.dll", CharSet = CharSet. Auto)]
Public static extern IntPtr SendMessage (IntPtr hWnd, int msg, int wParam, IntPtr lParam );
 
Public const int WM_SYSCOMMAND = 0x0112;
Public const int SC _CLOSE = 0xF060;
 
Public delegate bool EnumThreadProc (IntPtr hwnd, IntPtr lParam );
 
[DllImport ("user32.dll", CharSet = CharSet. Auto)]
Public static extern bool EnumThreadWindows (int threadId, EnumThreadProc pfnEnum, IntPtr lParam );
 
[DllImport ("user32.dll", CharSet = CharSet. Auto)]
Public static extern IntPtr find1_wex (IntPtr parent, IntPtr next, string sClassName, IntPtr sWindowTitle );
 

/* ------- Using HOTKEYs in a C # application -------
 
In form load:
Bool success = RegisterHotKey (Handle, 100, KeyModifiers. Control | KeyModifiers. Shift, Keys. J );
 
In form closing:
UnregisterHotKey (Handle, 100 );

 
Protected override void WndProc (ref Message m)
{
Const int WM_HOTKEY = 0x0312;

Switch (m. Msg)
{
Case WM_HOTKEY:
MessageBox. Show ("Hotkey pressed ");
Break;
}
Base. WndProc (ref m );
}
 
------- Using HOTKEYs in a C # application -------*/
 
[DllImport ("user32.dll", SetLastError = true)]
Public static extern bool RegisterHotKey (IntPtr hWnd, // handle to window
Int id, // hot key identifier
KeyModifiers fsModifiers, // key-modifier options
Keys vk // virtual-key code
);

[DllImport ("user32.dll", SetLastError = true)]
Public static extern bool UnregisterHotKey (IntPtr hWnd, // handle to window
Int id // hot key identifier
);
 
[Flags ()]
Public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
}
}

Related Article

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.