SN Fast input tool written in C # code

Source: Internet
Author: User

General software to enter the serial number (SN), and people usually use the most is probably pirated software, usually the serial number of pirated software (SN) are saved as: xxxxx-xxxxx-xxxx-xxxx form.

The place where the software enters the serial number is usually composed of several text boxes (textbox). Copying xxxxx into a text box will be very cumbersome. The SN fast input tool is then produced.

Of course, this has nothing to do with my reason for writing this program. The reason I wrote this program is purely because there is a netizen and his uncle Bet to write a program, and his uncle is to him to write this program, but unfortunately my friend is a programming beginner (more than I dish rookie), of course, can not complete this seemingly simple, practical use of many programming knowledge of the program.

To do this program, of course, the first thing is to understand the function of the program. Its function is to let you copy the form such as "Xxxxx-xxxxx-xxxx-xxxx" after the serial number, when you point the mouse pointer to the text box, the program can automatically add XXXXX to the corresponding text box.

Now that we're dealing with the copied serial number, we're definitely going to need something related to the Clipboard. Clipboard, fortunately this I used to use in C # for N times, no longer check the Windows API. The Clipboard class is provided in C #.

So I used the static method of String Clipboard.gettext (), took out the serial number of the band that I just copied, and then saved it in my program with a variable of type string Strkeys in order to use it.

The first step is to fetch the data from the Clipboard and we're done.

Next, we should consider how to deal with our data, our data is finally written to a few consecutive text boxes, then we can consider String.Split (char[],string splitoption) This method to divide the serial number into several substrings, The text is then output to the corresponding textbox handle via the Windows API. However, this will undoubtedly increase the difficulty of the program, a few consecutive text box switching, using the TAB key can be done, and then the text output to the text box, directly to the keyboard to play out on the OK. So obviously, we just have to simulate the key we're going to press, and this is when I first think of the keyboard emulation event keybd_event in the Windows API, so I started querying the Keybd_event method in MSDN, and there was a keyeventf_ in the method. KeyUp This parameter, but I don't know his corresponding value, so I start looking for the value of this long shaping. But I couldn't find it at all, and when I looked for keyup related stuff on MSDN, I suddenly discovered the System.Windows.Form.SendKeys class. The. NET Framework already encapsulates the keybd_event method of this unmanaged object into the SendKeys class, which can be simulated by using the SendKeys class directly.

The other way to query the TAB key is {Tab}.

Then I just have to convert the original text in the Strkeys-all to {Tab} and then to sendkeys this class to deal with, this program is basically completed.

So there's a

Strkeys.replace ("-", "{tab}");
Sendkeys.send (Strkeys);

These two lines of code.

This will have the main process of my program:

private void Processhotkey ()//Master Handler
Strkeys = Clipboard.gettext ();
Strkeys.replace ("-", "{tab}");
Sendkeys.send (Strkeys);

But how do we use shortcut keys to trigger, to complete this process.

So I started looking at Baidu and MSDN for information about Windows APIs that deal with global shortcut keys.

To set a shortcut key, you must use the two methods below user32.dll.

BOOL RegisterHotKey (
HWND hwnd,
int ID,
UINT Fsmodifiers,


BOOL Unregisterhotkey (
HWND hwnd,
int ID

Converted to C # code, the first thing to do is to reference the namespace System.Runtime.InteropServices; to load the unmanaged class user32.dll. So there was:

[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

This is the method of registering and uninstalling global shortcut keys, then we just need to add the statement that registers the shortcut key when Form_Load, when formclosing, unload the global shortcut key. At the same time, in order to ensure that the contents of the Clipboard is not affected by other programs to call the Clipboard interference, in the Form_Load, I first empty the contents of the Clipboard.

So there was:

private void Form1_Load (object sender, System.EventArgs e)
Label2. AutoSize = true;

Clipboard.clear ();//Clear the Clipboard first to prevent the Clipboard from copying other content first
RegisterHotKey (Handle, 0, KEYS.F10);

private void Form1_formclosing (object sender, FormClosingEventArgs e)
Unregisterhotkey (Handle, 100);//Uninstall shortcut keys

So what do we do in other windows, how do I get the shortcut key to call my main process Processhotkey ()?

Then we have to rewrite the WndProc () method to invoke the procedure by monitoring system messages:

protected override void WndProc (ref message m)//Monitor Windows messages
const int Wm_hotkey = 0x0312;//Press shortcut key
Switch (m.msg)
Case Wm_hotkey:
Processhotkey ();//Call Master Handler
Base. WndProc (ref m);

So my program is done.

SN Fast input tool written in C # code

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: 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.