Solved

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

Posted on 2008-10-18
7
1,163 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

895 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

13 Experts available now in Live!

Get 1:1 Help Now