Question

Is it possible to get the character if "Shift" was Pressed, that is, if I press "1" I get the "1" character, but if I hold down "Shift" it becomes "!" - all without the quotes of course. How do I do something like this programatically?

There was a method where you could just add to the ASCII code. However, this option is not suitable as it won't work in every locale.

Is there an option which will work in .NET, and possibly in Silverlight, where I can pass in a character like "9" and get the result "("?

Programmatically pressing "Shift" will not work in this case nor will any SendKeys based solution due to platform limitations.

This would be for a virtual keyboard, like the on-screen keyboard in Silverlight.

Was it helpful?

Solution

Couldn't you just spend a few minutes making a mapping table of your own?

1->!
2->@
3->#
etc. etc. etc.

OTHER TIPS

Odds are you'll have to do quite a bit of API groveling.

You can start here: http://msdn.microsoft.com/en-us/library/aa920537.aspx

More good reading: http://www.microsoft.com/globaldev/handson/dev/unicode-kbdsonwindows.pdf

I'd like to return the question: how do you know what keyboard layout is used? I.e., on one of my systems, the number 2 has the @-sign. On another, it has the "-sign (double quote). You say the 9 has the (-sign, but mine has the (-sign on top of the 0 and the )-sign on top of the minus-sign.

Windows comes by default with hundreds of keyboard mappings (i.e., Uzbek Cyrillic, US International, US Default, Dutch, German and German IBM etc). AZERTY vs QWERTY and so on.

The real question could be: how to read a keyboard mapping in windows programmatically. I don't know. While it will be possible if you find out how you can interface with the driver files, you'll probably be quicker off using a mapping based on, for instance, the layouts in my first link above. When you read the keyboard type from the registry, it'll cover the majority of cases.

Alternatively, you can consider programmatically setting the sticky keys for Shift (set sticky keys, send Shift key message and you're done).

(note that ASCII has nothing to do with keyboard scan codes. While it might've worked for some systems, it'd never have worked reliably for all.)

EDIT: consider trying the keyboard layout creator from Microsoft. Following it's creation process and a bit of reverse engineering, it can't be hard to find out how keyboard layouts are written. Yet, if all you need is support for one keyboard layout, take that one in the editor, reverse the Shift-action and simply programmatically set your newly created keyboard layout (and change it back) before and after someone starts typing.

For exactly that purpose, WPF and SilverLight offer the TextInput event. The character entered will be contained in the TextCompositionEventArgs.Text property. The event already considers user locale and keyboard layout:

private void Window_TextInput(object sender, TextCompositionEventArgs e)
{
    // e.Text contains the character that corresponds to the
    // key combination that was pressed
    Trace.WriteLine("TEXT: " + e.Text);
}

This actually might not solve your problem, since you specifically asked about a conversion function. I'm not aware of such a function in WPF/Silverlight (There is the KeyConverter class, but its purpose is a different one; you can use it to map both number keys and number keys on the numpad to the same key). However, you must be getting the key codes from somewhere, and if that's the keyboard this event should perfectly do.

In Windows Forms applications (which seems not relevant here though) the corresponding event to get the actual character would be Control.KeyPress.

find the math in behind the numbers of each character and work with it...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top