C # Implementation on-Screen keyboard (Screenkeyboard)

Source: Internet
Author: User
Tags static class thread

To implement a on-screen keyboard, all keyboard events need to be monitored regardless of whether the form is active or not. So you need a global hook, which is the system-wide hook.

What is a hook (hooks)

Hooks are a message processing mechanism platform provided by Windows, a function that is pre-initiated before receiving information in the normal running of a program is used to check and modify the information that is passed to the program (the hook) is actually a segment of the program that handles the message, and it is put into the system by the system call. Whenever a particular message is sent, the hook program captures the message before it reaches the destination window, which means that the hook function gets control first. The hook function can then process (change) the message, or it can continue to deliver the message without processing, and can force the end of the message to be passed. Note: Installing the hook function will affect the performance of the system. The system hooks for monitoring system-wide events are particularly noticeable. Because the system will call your hook function when it handles all related events, your system will obviously slow down. It should be used carefully and unloaded immediately after use. Also, because you can intercept messages from other processes in advance, it will affect other processes once your hook function is out of the question.

The scope of the hook

A total of two types of hooks, local and remote. Local hooks only hook up events of their own process. A remote hook

The child can also hook up events that occur in other processes. There are two kinds of remote hooks: a thread-based hook captures events from a particular thread in another process. In short, it is an event that can be used to observe the occurrence of a particular thread in another process. A system-wide hook captures the event messages that will occur for all processes in the system.

Hook type

There are 14 types of hooks in Windows, and each type of hook enables an application to monitor different types of system message processing mechanisms. The following describes the timing of all available hook types. For more information on MSDN, here are just two types of hooks we'll be using.

(1) Wh_keyboard_ll Hook

The Wh_keyboard_ll hook monitors keyboard messages entered into thread message queues.

(2) Wh_mouse_ll Hook

The Wh_mouse_ll hook monitors mouse messages entered into thread message queues. The following class encapsulates API calls for invocation.

1//NativeMethods.cs


2using System;


3using System.Runtime.InteropServices;


4using System.Drawing;


5


6namespace CnBlogs.Youzai.ScreenKeyboard {


7 [StructLayout (layoutkind.sequential)]


8 internal struct Mouseinput {


9 public int dx;


public int dy;


one public int mousedata;


public int dwflags;


public int time;


public IntPtr dwExtraInfo;


15}


16


[StructLayout (layoutkind.sequential)]


internal struct Keybdinput {


public short wvk;


public short Wscan;


public int dwflags;


public int time;


public IntPtr dwExtraInfo;


24}


25


num [StructLayout (layoutkind.explicit)]


internal struct Input {


[FieldOffset (0)]


public int type;


[FieldOffset (4)]


public mouseinput mi;


[FieldOffset (4)]


public Keybdinput Ki;


[FieldOffset (4)]


public hardwareinput Hi;


36}


37


[StructLayout (layoutkind.sequential)]


internal struct Hardwareinput {


-public int umsg;


Wparaml;


wparamh;


43}


44


internal class INPUT {


Public const int MOUSE = 0;


Public const INT keyboard = 1;


Public const INT hardware = 2;


49}


50


Wuyi internal Static class NativeMethods {


[DllImport ("User32.dll", CharSet = CharSet.Auto, SetLastError = False)]


internal static extern IntPtr GetWindowLong (IntPtr hWnd, int nindex);


54


[DllImport ("User32.dll", CharSet = CharSet.Auto, SetLastError = False)]


internal static extern IntPtr SetWindowLong (IntPtr hWnd, int nindex, int dwnewlong);


57


[DllImport ("User32.dll", EntryPoint = "SendInput", CharSet = CharSet.Auto)]


internal static extern UInt32 SendInput (UInt32 ninputs, input[] pinputs, Int32 cbsize);


60


[DllImport ("Kernel32.dll", EntryPoint = "GetTickCount", CharSet = CharSet.Auto)]


internal static extern int GetTickCount ();


63


[DllImport ("User32.dll", EntryPoint = "Getkeystate", CharSet = CharSet.Auto)]


Internal static extern short getkeystate (int nvirtkey);


66


[DllImport ("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]


internal static extern IntPtr SendMessage (IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);


69}


70}

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.