SoFunction
Updated on 2025-03-07

Example of SN fast input tool implemented by C#

This article describes the method of the SN fast input tool implemented by C#. Share it for your reference. The specific implementation method is analyzed as follows:

Generally, software requires inputting a serial number (SN), and the one that everyone uses most is probably pirated software. Usually, the serial number (SN) of pirated software is saved in the form of: XXXXX-XXXXX-XXXX-XXXXXX-XXXXXX.

The place where the software enters the serial number is usually composed of several text boxes (TextBox). Copying XXXX to the text box one by one will be very troublesome. So the SN fast input tool was created.

Of course, these have nothing to do with the reason why I wrote this program. The reason I wrote this program was purely because a netizen bet with his uncle that he wanted to write a program, and his uncle asked him to write this program. Unfortunately, my netizen is a programming beginner (a rookie who is more greedy than me). Of course, it is impossible to complete this seemingly simple program, but actually requires a lot of programming knowledge.

To make this program, of course, you must understand the functions of the program. Its function is to allow you to copy the serial number of the form "XXXXX-XXXX-XXXX-XXXX" and when you point your mouse to the text box, the program can automatically add XXXXX to the corresponding text box.

Since we are going to deal with the copied serial number, we must use clipboard-related things. Fortunately, I have used this in C# N times before, so I don’t need to check the Windows API anymore. C# originally provides the Clipboard class.

So I used the static method of string () to take out the serial number with - copied just now, and then save the variable strKeys of type string in my program for use.

The first step is to get the data from the clipboard and we are done.

Next, we should consider how to process our data. Our data will finally be written into several consecutive text boxes. Then we can consider splitting the serial number into several substrings through the method (char[], string splitoption), and then outputting the text to the corresponding textbox handle through the Windows API. However, doing this undoubtedly increases the difficulty of the program. You can switch several consecutive text boxes by using the Tab key, and then output the text to the text box and directly type the keyboard. Then it is obvious that we only need to simulate the key we want to press. At this time, the first thing I think of is the keyboard simulation event keybd_event in the Windows API. So I started to query the keybd_event method in MSDN. There is a KEYEVENTF_KEYUP parameter in the method, but I don't know its corresponding value, so I started to look for the value of this long plastic surgery. But I can't find it all the time. Just when I was looking for something related to KEYUP in MSDN, I suddenly found this class. It turns out that the .net framework has encapsulated the unmanaged object method of keybd_event into the SendKeys class. You can simulate keyboard operations by directly using the SendKeys class.

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

Then I just convert all the original text strKeys into {Tab} and then hand it over to the SendKeys class for processing, and the program will be basically completed.

So there is

Copy the codeThe code is as follows:
("-", "{TAB}");
(strKeys);

These two lines of code.

This way, the main process of my program:

Copy the codeThe code is as follows:
private void ProcessHotkey()//main handler
{
 strKeys = ();
 ("-", "{TAB}");
 (strKeys);
}

But how do we use shortcut keys to complete this process?

So I started to search for information about Windows APIs that process global shortcut keys in Baidu and MSDN.

To set shortcut keys, you must use the following two methods.

Copy the codeThe code is as follows:
BOOL RegisterHotKey(
 HWND hWnd,
 int id,
 UINT fsModifiers,
 UINT vk
);

and

Copy the codeThe code is as follows:
BOOL UnregisterHotKey(
 HWND hWnd,
 int id
);

To convert to C# code, then first you need to reference the namespace; to load the unmanaged class. So there is:

Copy the codeThe code is as follows:
[DllImport("", 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("", 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. So we only need to add a statement to register shortcut keys when Form_Load, and uninstall global shortcut keys when FormClosing. At the same time, in order to ensure that the content of the clipboard is not interfered by other programs calling the clipboard, when Form_Load, I first clear the content in the clipboard.

So there is:

Copy the codeThe code is as follows:
private void Form1_Load(object sender, e)
{
  = true;

();//Clear the clipboard first to prevent other content from being copied in the clipboard first
 RegisterHotKey(Handle, 100, 0, Keys.F10);
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(Handle, 100);//UnregisterHotKey
}

So in another window, how can we call my main process ProcessHotkey() after pressing the shortcut key?

Then we must rewrite the WndProc() method and call the process by monitoring system messages:

Copy the codeThe code is as follows:
protected override void WndProc(ref Message m)// Monitor Windows messages
{
const int WM_HOTKEY = 0x0312;//Press the shortcut key
 switch ()
 {
case WM_HOTKEY:
ProcessHotkey();//Calling the main handler
 break;
 }
 (ref m);
}

At this point our program is completed.

I hope this article will be helpful to everyone's C# programming.