Analysis of several key technologies for making a keyboard by yourself

Source: Internet
Author: User

In embedded applications and some security software, you often do not need to input through the physical keyboard. Although Microsoft provides a soft keyboard, this software disk cannot be customized and the interface cannot automatically detect whether the current cursor is in the input state, so sometimes we need to implement this soft keyboard on our own. This article will explain several key technologies involved in implementing the soft keyboard.

1. Implementation of floating forms

The form of the soft keyboard is different from the normal form. When the form becomes the current form, the cursor focus of the form of other processes is not affected. That is to say, although the form is currently activated, the cursor is still on the form of other processes.


As shown in, although the keyboard is in front of the notepad, the cursor is still in the notepad.

To implement this technology, we must set the current form as a floating toolbar. Here I will provide the implementation method of C # winform:

  private   const   int  ws_ex_toolwindow = 0x00000080; 
private const int ws_ex_noactivate = 0x08000000;
protected override createparams
{
Get
{< br> createparams CP = base . createparams;
CP. exstyle | = (ws_ex_noactivate | ws_ex_toolwindow);
CP. parent = intptr. zero; // keep this line only if you used usercontrol
return CP;
// return base. createparams;
}< BR >}

AboveCodeIs to specify winform as a floating toolbar form. You only need to reload the createparams function in the winform class and write it according to the above Code.

Ii. How to check whether the current input is in the input state

In some embedded devices, we do not have a physical keyboard, and all input is input through the touch screen and soft keyboard. At this time, we must bring up the keyboard only when it is in the input state. Otherwise, if the keyboard is always on the interface, it will be neither beautiful nor affect otherProgram.

To implement this function, we can think of the most direct method: whether Windows will send an event when it is currently in the input state, or through what hook program. But I have studied it for a long time and have not found this method. If anyone knows this method, tell me in the reply.

The method I have found is to regularly ask if the current Windows form is in the input state (http://www.my400800.cn ).

 intptr hwnd = getforegroundwindow (); 
uint processid;
uint threadid = getwindowthreadprocessid (hwnd, out processid );
guithreadinfo lpgui = New guithreadinfo ();
lpgui. cbsize = marshal. sizeof (lpgui);
If (getguithreadinfo (Threadid, ref lpgui ))
{< br> If (lpgui. hwndcaret! = 0)
{< br> return hwnd;
}< BR >}

As shown in the above Code

First, we get the handle of the current form through the getforegroundwindows API. Then, we can use getguithreadinfo to obtain some attributes of the current form. These attributes are defined in guithreadinfo.

  Public   struct  guithreadinfo 
{< BR> Public int cbsize;
Public int flags;
Public int hwndactive;
Public int hwndfocus;
Public int hwndcapture;
Public int hwndmenuowner;
Public int hwndmovesize;
Public int hwndcaret;
Public system. drawing. rectangle rccaret;
}

The above is guithreadinfo.
Structure. We can use this information to obtain the subwindow handle of the current focus in the current form, the subwindow handle of the cursor, And the subform handle that is being activated. Here we only need to use the current cursor
Is hwndcaret. If the value of hwndcaret is not 0, it indicates that the current form is inputting.

The C # definition of related API functions is as follows:

 [dllimport ( "user32.dll" )] 
[ return : financialas (unmanagedtype. bool)]
Public static extern bool getguithreadinfo ( uint idthread, ref guithreadinfo lpgui);
[dllimport ( "user32.dll" )]
Public static extern intptr getforegroundwindow ();
[dllimport ( "user32.dll" , setlasterror = true )]
static extern uint getwindowthreadprocessid (intptr hwnd, out uint lpdwprocessid);
3. Simulate keyboard input

Simulating keyboard input is relatively simple.. NET provides a static function to simulate keyboard input.

System. Windows. Forms. sendkeys. Send

This function is very simple, and Microsoft's help is comprehensive. I will not talk about it here.

In addition, we can use more underlying API functions to simulate keyboard input.

 [dllimport ( "user32.dll" )] 
static extern void keybd_event ( byte bvk, byte bscan, uint dwflags,
int dwextrainfo);

This function is keybd_event. Regarding the use of this function, Microsoft's help is also clearly written, and we will not repeat it here.

As shown in the above Code

First, we get the handle of the current form through the getforegroundwindows API. Then, we can use getguithreadinfo to obtain some attributes of the current form. These attributes are defined in guithreadinfo.

  Public   struct  guithreadinfo 
{< BR> Public int cbsize;
Public int flags;
Public int hwndactive;
Public int hwndfocus;
Public int hwndcapture;
Public int hwndmenuowner;
Public int hwndmovesize;
Public int hwndcaret;
Public system. drawing. rectangle rccaret;
}

The above is guithreadinfo.
Structure. We can use this information to obtain the subwindow handle of the current focus in the current form, the subwindow handle of the cursor, And the subform handle that is being activated. Here we only need to use the current cursor
Is hwndcaret. If the value of hwndcaret is not 0, it indicates that the current form is inputting.

The C # definition of related API functions is as follows:

 [dllimport ( "user32.dll" )] 
[ return : financialas (unmanagedtype. bool)]
Public static extern bool getguithreadinfo ( uint idthread, ref guithreadinfo lpgui);
[dllimport ( "user32.dll" )]
Public static extern intptr getforegroundwindow ();
[dllimport ( "user32.dll" , setlasterror = true )]
static extern uint getwindowthreadprocessid (intptr hwnd, out uint lpdwprocessid);
3. Simulate keyboard input

Simulating keyboard input is relatively simple.. NET provides a static function to simulate keyboard input.

System. Windows. Forms. sendkeys. Send

This function is very simple, and Microsoft's help is comprehensive. I will not talk about it here.

In addition, we can use more underlying API functions to simulate keyboard input.

 [dllimport ( "user32.dll" )] 
static extern void keybd_event ( byte bvk, byte bscan, uint dwflags,
int dwextrainfo);

This function is keybd_event. Regarding the use of this function, Microsoft's help is also clearly written, and we will not repeat it here.

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.