Click here to Skip to main content
15,906,574 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello to everybody! i am working on a project, which is going to be something like that. So i've made the virtual keyboard by using div tags and i want every key the user is pressing to be highlited in the virtualKB. I've tried for some days working in the idea to have onkeypress event in the div tags, so when a key is pressed to make JS validation to see which key to highlite. I've come to an end with that solution cause i tried everything but its not working. I would be really appreciated if anybody could give me an alternative idea about that.
Thanks in advance!
Posted
Comments
Sergey Alexandrovich Kryukov 24-Oct-13 20:13pm    
Not clear. What exactly did you try? What's the problem? Why "validation"? The idea is quite simple, hard to understand what are you possibly missing...
—SA
pedrag 25-Oct-13 7:29am    
thanks a lot for the advices!!
Sergey Alexandrovich Kryukov 25-Oct-13 9:59am    
I actually described the whole solution in my answer. Will you accept it formally?
—SA
pedrag 25-Oct-13 8:04am    
Seeing your answer (excepts the JQuery part) the problem is tha same as my thinking. I wanna have a general function, which is gonna do the check for whick key is pressed. This is my big problem. I tried some ideas that came out, but with no luck.
Sergey Alexandrovich Kryukov 25-Oct-13 10:00am    
Programming is not a matter of luck. Not at all.

Now, what would be a problem? Do you have further questions? I described all parts of what you need to do, you only have to put it all together...

—SA

1 solution

Don't use onkeypress event (this is how it is really called), use onkeydown (to highlight) and onkeyup (to stop highlighting).
Put all the keys in some element and find each key using DOM navigation, to avoid such a tedious thing as giving unique IDs to all keys. One good way to do it, as well as adding event handling and writing the handlers themselves, is using jQuery. Please see:
http://api.jquery.com/children/[^],
http://api.jquery.com/each/[^],
http://api.jquery.com/keydown/[^],
http://api.jquery.com/keyup/[^].

For highlighting, use two different CSS styles and toggle them back and forth on onkeydown/onkeyup events:
http://api.jquery.com/addClass/[^],
http://api.jquery.com/removeClass/[^],
http://api.jquery.com/toggleClass/[^],
http://api.jquery.com/category/css/[^].

If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery[^],
http://jquery.com[^],
http://learn.jquery.com[^],
http://learn.jquery.com/using-jquery-core[^],
http://learn.jquery.com/about-jquery/how-jquery-works[^] (start from here).

—SA
 
Share this answer
 

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