Click here to Skip to main content
15,887,946 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Good evening!

I developed a custom textbox from scratch (inheriting Windows.Forms.Control only).
All working fine! (selections, cursors, commands, text scrolling, etc)

my problem is to map the keyboard correctly. I wrote a class (key x value) for identify the keyboard;
Well, the problem is: the map of keyboard is totally manually, like this example of ABNT layout

VB
''' <summary>
''' Specifies a keyboard layout ABNT2
''' </summary>
Public Class KeyboardLayoutAbnt2
    Inherits KeyboardLayout

#Region "Methods"
     Private Sub SetKeys()

        '//numbers 
        '// KeyEntry Constructor: 
        '// Sub New(keycode As Integer, keyvalue As Char, shiftedchar As Char)
        Call Add(New KeyEntry(Keys.D1, "1", "!", "¹"))
        Call Add(New KeyEntry(Keys.D2, "2", "@", "²"))
        Call Add(New KeyEntry(Keys.D3, "3", "#", "³"))
        Call Add(New KeyEntry(Keys.D4, "4", "$", "£"))
        Call Add(New KeyEntry(Keys.D5, "5", "%", "¢"))
        Call Add(New KeyEntry(Keys.D6, "6", "¨", "¬"))
        Call Add(New KeyEntry(Keys.D7, "7", "&"))
        Call Add(New KeyEntry(Keys.D8, "8", "*"))
        Call Add(New KeyEntry(Keys.D9, "9", "("))
        Call Add(New KeyEntry(Keys.D0, "0", ")"))

        Call Add(New KeyEntry(Keys.NumPad1, "1", "!", "¹"))
        Call Add(New KeyEntry(Keys.NumPad2, "2", "@", "²"))
        Call Add(New KeyEntry(Keys.NumPad3, "3", "#", "³"))
        Call Add(New KeyEntry(Keys.NumPad4, "4", "$", "£"))
        Call Add(New KeyEntry(Keys.NumPad5, "5", "%", "¢"))
        Call Add(New KeyEntry(Keys.NumPad6, "6", "¨", "¬"))
        Call Add(New KeyEntry(Keys.NumPad7, "7", "&"))
        Call Add(New KeyEntry(Keys.NumPad8, "8", "*"))
        Call Add(New KeyEntry(Keys.NumPad9, "9", "("))
        Call Add(New KeyEntry(Keys.NumPad0, "0", ")"))

        '//operations
        Call Add(New KeyEntry(Keys.Divide, "/", ""))
        Call Add(New KeyEntry(Keys.Multiply, "*", ""))
        Call Add(New KeyEntry(Keys.Subtract, "-", ""))
        Call Add(New KeyEntry(Keys.Add, "+", ""))
        Call Add(New KeyEntry(Keys.Decimal, ".", ""))


I want to create the control using the key handling from the windows because in the future i'll try to implement the IME.

Please, anyone can help me with this challenge. I'm losing all my hairs
Posted
Comments
Sergey Alexandrovich Kryukov 12-Mar-13 21:03pm    
Very, very, very bad! Makes no sense at all.

Whenever you handle a key press, the even handle already gives you the character code you can use immediately. That is, none of your lines is needed, ever. Handle general keypress.
If have more questions, you are welcome to ask, but you need to tag what UI library or application type do you use.

—SA
Estevão C Souza 12-Mar-13 22:16pm    
hi!
you are correct if we talking about KeyDown and KeyUp events, but in some languages like portuguese the layout of keyboard is different.
Example: KeyUp returns a Keys.Decimal the way correct will be a dot (.), but in ABNT2 layout Keys.Decimal corresponding value is the comma (,) because of this, i used this way 'to solve' partially my problem.
Sergey Alexandrovich Kryukov 12-Mar-13 22:51pm    
No! If you are talking about KeyDown event, you are right, but if you handle KeyPressed, you will receive character, something which was already converted from the key code, and then the layout is irrelevant, you will receive the character according to the really used input language.

And you by the way, would totally ignore the code layout. You would hard-code characters, so your code would ignore input language... Look, stop doing what you are doing, can you finally realize that?
—SA
Estevão C Souza 13-Mar-13 7:57am    
Ok!, you're right! KeyPress returns the char, but in my necessity i need handle the KeyDown and KeyUp, because of this i asked 'how to handle keys based on windows layout' in my question. I didn't understand what are you want to say with UILibrary, but i'm using only Control inheritance and Drawings to draw my control.

for info, i'm based my 'idea' on http://neoforce.codeplex.com/releases/view/52549 (CustomControl for XNA Framework)
Sergey Alexandrovich Kryukov 13-Mar-13 11:37am    
OK, why your "but" is but? Handle those, too, why not? Handle special hot keys (or something) via KeyDown (why KeyUp? well, sometimes, too), and KeyPress for characters...
—SA

1 solution

Please use the event KeyPress as it gives you the character of the letters. It will preserve the system keyboard layouts, so you don't need to deal with each key separately. Use KeyDown to work with keys on lower level, such as F1, F2, etc.

—SA
 
Share this answer
 
Comments
Espen Harlinn 13-Mar-13 12:07pm    
Good point :-D
Sergey Alexandrovich Kryukov 13-Mar-13 13:34pm    
Thank you, Espen.
—SA

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