Search code examples
c#opentk

Getting the accurate character from keys pressed


I'm working with an OpenTK window and I am trying to create a hand-made text-box for a GUI.

The issue I'm running into here is getting proper keyboard input.

For example, when the user types Shift + 1, the character that appears in the text-field is an exclamation mark.

The issue is, using input libraries just give me raw key presses. ie, pressing Shift + 1 gives you an event where shift is being pressed, and an event where 1 is being pressed.

I'm looking to know if there is a way to convert these presses into the appropriate character, so that they can use whatever special characters they have available on their keyboard.

If this feature is not available - would the best approach be to hard-code the appropriate keys when modifiers are used with them?


EDIT: I changed the event I was using from KeyDown to KeyPress - it gives a different event which handles the modifiers correctly. Thank you for all the help!


Solution

  • use keypress EventHandler of Your control then

    if(e.keychar==(char)keys.[specified key])do some work
    

    e is Argument of keypress eventhandler function