tags:

views:

119

answers:

1

Hi,

I'm quite confused about handling of Key http://msdn.microsoft.com/en-us/library/system.windows.input.key.aspx in a KeyEventArgs.
For example: I want to handle colon key (made on my Italian keyboard with dot + Shift), so I write some code like this

if ( e.Key == Key.OemPeriod && e.KeyboardDevice.Modifiers == ModifierKeys.Shift) {
    // Code 
}

But this approach isn't correct because work only with Italian keyboard. What is the correct and culture independent way to handle Key in WPF?

+2  A: 

Have you tried handling the PreviewTextInput or TextInput events instead? These events should give you the text entered (which is device-independent) as opposed to the physical key pressed (which is device-dependent).

Christian Hayter
PreviewTextInput and TextInput provides the correct info. When I have made my test the TextInput event is trapped elsewhere and don't reach my control, but PreviewTextInput works fine.Thanks
Matteo Valdina
My question is not only how to get char from a TextBox. Is more generally a question about the correct way to made this conversion from Keyboard event to char representation. Because I found very strange a kind of culture depended limit in the Keyboard handling infrastracture.
Matteo Valdina
Those events are just the WPF wrapper around the underlying Windows messages. You get the same sequence of events in WinForms, VB, MFC, WinAPI, etc, only the event/message names differ. Windows has always worked like that AFAIK.
Christian Hayter