Solved

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

Posted on 2008-10-18
7
1,164 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
ID: 22751311
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
ID: 22751320
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
ID: 22752572
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 3

Accepted Solution

by:
BTecho earned 500 total points
ID: 22758650
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
ID: 22760820
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
ID: 31507516
Excelent advice from BTecho. Thanks a lot!!!
0
 
LVL 3

Expert Comment

by:BTecho
ID: 22764038
Very glad I could be of help :)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
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 connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

770 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