seoul
seoul

Reputation: 864

Keyboard hook in c#

I want to develop a keystroke converter which will convert any keystroke into my local language. For example, if user type "a" then it will be replaced with it's corresponding unicode letter "\u0995"

I used a code similar to: https://stackoverflow.com/global-low-level-keyboard-hook-freezing-in-c-net-3-5 There, i edited as follows:

 // MessageBox.Show("Test"); // I do not want this so commented
 int vkCode = Marshal.ReadInt32(lParam);
 Console.WriteLine((Keys)(vkCode + 2));

 SendKeys.Send("mmm"); // mmm will be my desired unicode character           

Now, i open any application and type anything i get both the typed letter and "mmm".

For example, if i type: abcd then i get output as: mmmcmmmdmmmemmmf .........[output]


Now my question is,

1) How can i edit this code to send a unicode letter instead of a letter ? ( I mean, if i type "p", then i want other applications should receive unicode character similar to this unicode character: "0996"

2) How to make sure only the unicode character is sent to other application, the typed character must not be appended. I mean, i don't want the unicode character and typed english letter as in the output above[output]

Upvotes: 1

Views: 2268

Answers (5)

Stepagrus
Stepagrus

Reputation: 1559

You can use SharpHook library.

SharpHook targets .NET 6+, .NET Framework 4.6.2+, and .NET Standard 2.0

SharpHook provides a cross-platform global keyboard and mouse hook for .NET, and the ability to simulate input events. It is a thin wrapper of libuiohook and provides direct access to its features as well as higher-level types to work with it.

Upvotes: 0

ePandit
ePandit

Reputation: 3243

1) How can i edit this code to send a unicode letter instead of a letter ?

SendKeys.Send() can send Unicode Indic characters too e.g.

SendKeys.Send("খ");

If you want to use Unicode code to send the character, then

SendKeys.Send('\u0996'.ToString());

2) How to make sure only the unicode character is sent to other application?

If the code is inside a KeyDown event function, you can suppress the actual key being typed by using following just after SendKeys.Send() statement:

e.Handled == true;

Upvotes: 1

Mike Clark
Mike Clark

Reputation: 10136

  1. Skip calling CallNextHookEx to prevent a particular key event from propagating.

  2. Re: using SendKeys to enter Unicode input: "If your application is intended for international use with a variety of keyboards, the use of Send could yield unpredictable results and should be avoided." -MSDN Basically I think SendKeys doesn't explicitly support sending Unicode input.

Have you looked at Microsoft Text Services Framework instead of this approach you are trying? I think it is basically purpose built to address what you are trying to do.

Upvotes: 0

Oliver Hanappi
Oliver Hanappi

Reputation: 12336

I'm not quite sure, but I think you can interrupt the chain if you don't call CallNextHookEx but return a null pointer instead. But that this is something you should rather not do usually ;)

Best Regards,
Oliver Hanappi

Upvotes: 0

VinayC
VinayC

Reputation: 49175

AFAIK, hooking is for monitoring, so not sure if its correct approach for what you are trying to do. But in your code, perhaps you can skip calling next hook "CallNextHookEx" and that may swallow the key typed. Mind you that you should swallow conditionally otherwise you may block keys such as ALT and CTL.

Upvotes: 0

Related Questions