Click here to Skip to main content
15,919,423 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi ,

I have written a normal Low-level keyboard hook. I am able to trap correctly all alphanumeric keys in it. But am unable to trap special characters like @,#,$,? etc. All these keys require pressing of the Shift Key followed by an other key. Example for ? , we need to press Shift+ / keys and for say @ , we need to press Shift and 2 key.

Is there anyway to trap such special characters? Some sample code will be really helpful. Please note that I am using C++ .

Thanks for your help.
Posted
Comments
Chris Meech 23-Feb-11 12:19pm    
How are checking for upper case or lower case? However you do that, the same applies for the 2 key being either a 2 character or an @ character.

You are confusing different abstraction level.
What you call "special characters" are not special at all: they're just regular characters. They differ like an "a" differs form an "A".
But the problem is the level of "input" you are using.
A low level hook knows noting about "characters": it knows only about "keys" (the physical ones), and they are identified by their names (That - conventionally- are the same of the main letter printed on them - hence the confusion).

Characters are the result of a mapping between sequences of key pressures/releases and keyboard layout, that may be different from country to country (for example, on my keyboard, Shift+2 is '"' and and ? id shift+' and @is Ctrl+Alt+ò
The task to map keys (or keychord) to characters is a task of the keybord driver, that handles the low level event you hooked.

If you want to manage characters use "character oriented input functions" like _getch() or std::cin or other console oriented character functions (in case of console app) or handle WM_CHAR (but ensure your messageloop calls TranslateMessage before DispatchMessage) in case of Win32 App.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 24-Feb-11 18:53pm    
Finally I see correct explanation, my 5.
--SA
CodyDaemon 25-Feb-11 7:30am    
Since the guy had written a LL keyboard hook, I assumed he wanted a solution that worked at that level. I agree that if they does need what using a hook gives him (global capture for example) then they should just drop back to WM_CHAR (and it friends) messages. Since they are the far easier way of dealing with the chars.
Emilio Garavaglia 25-Feb-11 7:54am    
Yes, thet's just the other side of the coin!
But ... if he's interested in low-level hook (just key pressures), why is he trying to get characters (that have a semantics)?
CodyDaemon 25-Feb-11 10:31am    
Well for a project I am doing, I am using a LL hook so I can have global hot keys that use another key as there meta key. i.e Using 'Home' + {Something} as the hotkey combo, but then translating the VK codes into the char code so I know that the key was the letter/number I want. ie. To catch Home+2.
Emilio Garavaglia 25-Feb-11 15:05pm    
Sure, but the point, here, is that "Shift+2" is not necessarily "@": it depends on the localization of the keyboard.
Give a look to "http://en.wikipedia.org/wiki/Keyboard_layout"
you might have to translate it using TranslateMessage api
 
Share this answer
 
I believe the way that you have to do this, well the way I did it atleast, was to check for the status of the shift keys when you get the 'other' key message. i.e If you wanted to catch '?', then trap '/', and test for shift keys being pressed. If both are true then do your trap stuff.
if(key=='/' && bShiftPressed)
{
    doTrapStuff();
}
 
Share this answer
 
Comments
Hellraiser123 23-Feb-11 10:55am    
I can do that if there are just one or 2 such keys. But what If i want to trap any special key that the user may press on the Keyboard? Am a complete newbie to all this hooks and keyboard trapping. So please bear with me.
Sergey Alexandrovich Kryukov 23-Feb-11 22:42pm    
At the level of hooks, there is not "special keys", forget it. Justin is right.
--SA
CodyDaemon 23-Feb-11 11:03am    
What other special keys do you mean? Have you looked at:- http://msdn.microsoft.com/en-us/library/dd375731(v=vs.85).aspx <-- List of Virtual Key codes.
Hellraiser123 23-Feb-11 21:06pm    
Yes. I dont want to do anything on press of say a special character like '@'. All I want to do is to store the special character like '@' in a buffer. So for that I need to trap the @ character. Thanks for replying.
CodyDaemon 24-Feb-11 6:27am    
I am not sure what you are trying to do, but if you just want to log the special keys then you can just use your fav. string/list class and just append it to the string/list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900