Solved

keybd_event not working with special characters like @, <, >, =, (, )

Posted on 2008-10-18
7
1,161 Views
Last Modified: 2013-12-04
I had been trying some sequences on notepad, without problem but when i started to test  with special characters like @, <, >, =, (, ), they simply don't apper.
What can i do to fix the problem?
0
Comment
Question by:robballi
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:BTecho
Comment Utility
Hi again :)

For @ symbol , simulate shift+2

    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event(0x32, 0, 0, 0);
    keybd_event(0x32, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);

VK_2 is not declared in the list of virtual key codes so C++Builder will complain so you can put the hex value in instead.

another example for ) character , shift+0

    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event(0x30, 0, 0, 0);
    keybd_event(0x30, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);

Here's a list of Virtual Key Codes
http://www.kbdedit.com/manual/low_level_vk_list.html
0
 
LVL 3

Expert Comment

by:BTecho
Comment Utility
I got curious so I checked and the winuser.h file in the borland folder has the defined and mapped out the vk codes.

There I saw a comment
/*
 * VK_0 - VK_9 are the same as ASCII '0' - '9' (0x30 - 0x39)
 * 0x40 : unassigned
 * VK_A - VK_Z are the same as ASCII 'A' - 'Z' (0x41 - 0x5A)
 */

for 0 instead of 0x30 you could also put, for the ( character

    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event('0', 0, 0, 0);
    keybd_event('0', 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
0
 

Author Comment

by:robballi
Comment Utility
Hi BTecho:

Nice to talk to you again.
I have to tell you about your kind solution i think only works for english keyboards, since form what i can see, your logic is that: Shift + 0 on english keyboard is ) and @ on english keyboard is Shift + 2. But in other languages it is not necesary the case!!
Besides i cannot still get:

for '< ' since Shift + ',' is <
i would suppose
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event(0x2C, 0, 0, 0);
    keybd_event(0x2C, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
unfortunatelly nothing happens!

for '>' since Shift + '.' is >
i would suppose
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event(0x2E, 0, 0, 0);
    keybd_event(0x2E, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
unfortunatelly nothing happens too!

for '='
i don't have any clue?

it seems that SendInput is superseding keybd_event, but i cannot find any specific piece of code of how to handle this on C++ Builder?

Thanks again.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 3

Accepted Solution

by:
BTecho earned 500 total points
Comment Utility
That piece of code you posted doesn't work for me either.

Please try this piece of code to get the correct values of the keys. Needs two TEdit controls on the form...

//-On the OnKeyDown Event for Edit1----------------------------------------------------------------------
void __fastcall TForm1::Edit1KeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
int keyint=Key;
Edit2->Text= IntToHex( keyint,2);
}
//---------------------------------------------------------------------------

The ",>"  key I found is  actually 0xBC . The Equals key is 0XBB without shift.
    keybd_event(0xBB, 0, 0, 0);
    keybd_event(0xBB, 0, KEYEVENTF_KEYUP, 0);

 I'm using an American/English based keyboard.
0
 

Author Comment

by:robballi
Comment Utility
Dear BTecho:

Thanks to your advice it seems that these are the solutions:

for caracter == '<'
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event(0xBC, 0, 0, 0);
    keybd_event(0xBC, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);

for caracter == '>'
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event(0xBE, 0, 0, 0);
    keybd_event(0xBE, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);

for caracter == '='
    keybd_event(0xBB, 0, 0, 0);
    keybd_event(0xBB, 0, KEYEVENTF_KEYUP, 0);

Excelent advice BTecho
0
 

Author Closing Comment

by:robballi
Comment Utility
Excelent advice from BTecho. Thanks a lot!!!
0
 
LVL 3

Expert Comment

by:BTecho
Comment Utility
Very glad I could be of help :)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now