[Reprint] C # register the shortcut key Ctrl + C Ctrl + V for the Application

Source: Internet
Author: User

In applications, we may need to implement shortcut keys such as Ctrl + C copy and CTRL + V paste. This article briefly introduces its implementation and provides an implementation class.

(1) create a class file named hotkey. cs. The Code is as follows:
Using system;
Using system. Collections. Generic;
Using system. runtime. interopservices;
Using system. Windows. forms;

Namespace koalastudio. bookshopmanager

{

Class hotkey

{

// If the function is successfully executed, the return value is not 0.

// If the function fails to be executed, the return value is 0. To get the extended error message, call getlasterror.

[Dllimport ("user32.dll", setlasterror = true)]

Public static extern bool registerhotkey (

Intptr hwnd, // handle of the window for which the hotkey is to be defined

Int ID, // define the hotkey ID (cannot be the same as other IDS)

Keymodifiers fsmodifiers, // identifies whether the hot key takes effect only when you press ALT, Ctrl, shift, or windows.

Keys VK // defines the content of the Hot Key

);

 

[Dllimport ("user32.dll", setlasterror = true)]

Public static extern bool unregisterhotkey (

Intptr hwnd, // handle of the window to which the hotkey is to be canceled

Int ID // ID of the hotkey to be canceled

);

 

// Defines the name of the secondary key (convert a number into a character for memory, or use a value directly for this enumeration)

[Flags ()]

Public Enum keymodifiers

{

None = 0,

Alt = 1,

CTRL = 2,

Shift = 4,

Windowskey = 8

}

}

}

Brief description:
The "public static extern bool registerhotkey ()" function is used to register hotkeys. This function can be used only after the dynamic link library of user32.dll is referenced.

User32.dll is a non-hosted code and cannot be directly referenced in the namespace. Therefore, you need to use "dllimport" before using it. Therefore, you need to add

"[Dllimport (" user32.dll ", setlasterror = true.
The "public static extern bool unregisterhotkey ()" function is used to deregister the hotkey. Similarly, you must use dllimport to reference user32.dll before using it.
"Public Enum keymodifiers {}" defines a set of enumerations. The numeric code of the secondary key is directly expressed as text for ease of use. In this way, we do not have to remember every secondary

You only need to select the name of the Code.

(2) take form forma as an example to introduce the use of the hotkey class.

Register the hotkeys in the activate event of forma. In this example, register the three hotkeys SHIFT + S, CTRL + Z, ALT + D. The ID number can be set as needed, but it must not be repeated.
Private void form_activated (Object sender, eventargs E)

{

// Register the hotkey SHIFT + s with the ID 100. Hotkey. keymodifiers. Shift can also be expressed directly by number 4.

Hotkey. registerhotkey (handle, 100, hotkey. keymodifiers. Shift, keys. s );

// Register the hotkey Ctrl + B with the ID 101. Hotkey. keymodifiers. CTRL can also be expressed directly by number 2.

Hotkey. registerhotkey (handle, 101, hotkey. keymodifiers. Ctrl, keys. B );

// Register the hot key Alt + D with the ID 102. Hotkey. keymodifiers. ALT can also be expressed by the number 1.

Hotkey. registerhotkey (handle, 102, hotkey. keymodifiers. ALT, keys. D );

}

// Deregister the hotkey in the forma leave event.

Private void frmsale_leave (Object sender, eventargs E)

{

// Deregister the hotkey setting with ID 100

Hotkey. unregisterhotkey (handle, 100 );

// Deregister the hotkey setting with ID 101

Hotkey. unregisterhotkey (handle, 101 );

// Deregister the hotkey setting with ID 102

Hotkey. unregisterhotkey (handle, 102 );

}

Reload the wndproc function in froma.
///
/// Monitor Windows messages
/// Reload the wndproc Method for hotkey response
///
///
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: // SHIFT + S
// Enter the shortcut key response code.
Break;
Case 101: // press Ctrl + B.
// Enter the shortcut key response code.
Break;
Case 102: // Alt + D is pressed
// Enter the shortcut key response code.
Break;
}
Break;
}
Base. wndproc (ref m );
}

After the code is completed, the program responds to any of the three Shortcut Keys SHIFT + S, CTRL + B, and ALT + D in the form.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/ismycxp/archive/2007/07/19/1698942.aspx

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.