views:

264

answers:

5

Is it possible to get the Character if "Shift" was Pressed, ie. if i press "1" I get the "1" character, but if I hold down "Shift" it becomes "!" - all without the quotes of course. I want to to 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, and works 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

+2  A: 

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

1->!
2->@
3->#
etc. etc. etc.
ceejayoz
Don't forget that there are different keyboard layouts (with possibly even different codes on different platforms such as Mac vs. Windows). E.g. on a German keyboard you would get double quotes on Shift+2.
0xA3
Eikern
Depends on the site needs. If he knows it'll be limited to one region, not a problem.
ceejayoz
Keyboard layout is irrespective (though related) to the region. I'm in a "Dutch region" and (unfortunately) deal with three types of keyboard layouts daily: US International on one, Dutch on another and German on the laptop.
Abel
This was something I thought about after asking this question - this may be acceptable, as the layout will be fixed (En-GB).I thought about this before a while ago however I'm converting an old solution to Silverlight and thought I'd ask if this was possible any other way.
RoguePlanetoid
+1. Its simple, it will work for a known keyboard layout and doesn't need access to APIs that are unlikely to be available in Silverlight.
AnthonyWJones
A: 

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.

Abel
A: 

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

Stu
Seems he is on Silverlight, so there won't be any Windows API :-(
0xA3
I've done this before in VB6 it worked fine - but as mentioned this API is not available in Silverlight.
RoguePlanetoid
A: 

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.

0xA3
It is possible to read the Shift and Key values when together, but not to write them as output e.g. Shift+2 gets " instead of 2
RoguePlanetoid
Sorry, I didn't understand your comment. What are you actually trying to achieve? If you need both, i.e. the keys pressed + corresponding character, you will have to listen to both (Preview)KeyDown and (Preview)TextInput events.
0xA3
I was not sure how to phrase my original question, so sorry if it was ambigious however I want to output to screen what the result of Shift + Character would be, and wondered if this was possible, as it may be of use to others. Getting the Shift Key+Character is possible using the KeyPress/KeyDown events even in Silverlight.
RoguePlanetoid
Where do you find the TextInput event in Silverlight??
AnthonyWJones
@AnthonyWJones: It is new in Silverlight 4.0. See http://msdn.microsoft.com/en-us/library/system.windows.uielement.textinput%28VS.96%29.aspx.
0xA3
I did not realise this was new to Silverlight 4.0 as I had not looked at them until this was mentioned. Guess I would have been stuck if I wanted to get the character and shift key combination pressed!
RoguePlanetoid
A: 

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

Chen Kinnrot