none
ascii value of special keys

    Question

  • Hi,

     

    how can I get the ASCII value of special keys as F4 or F10?

    I know I can use Convert.ToUInt32('A') for regular keys, but i can't find a way to convert special keys.

     

    Thanks,

    Gidi.

    Tuesday, March 22, 2011 2:30 PM

Answers

  • Hi Gidi,

    Based on your previous post, I think you want to convert the special keys to win32 virtual key codes.

    I am not quite sure what's the original type of the "special key". In a WPF application, you might catch it in a keyboard event as System.Windows.Input.Key. Then you can use KeyInterop.VirtualKeyFromKey method to convert it to a virtual key code.

    If the "special key" is string, you can try Enum class's TryParse or Parse method to convert the string to a WPF key, and use the above method to further convert it to a virtual key code.

    If the "special key" is winform key, you can simply use Convert.ToUInt32 to convert it to a virtual key code.

    Hope this helps.


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by gidi Wednesday, March 23, 2011 9:42 AM
    Wednesday, March 23, 2011 3:10 AM
    Moderator

All replies

  • Function keys are not standard ascii values.
    Tuesday, March 22, 2011 2:42 PM
  • Hi Gidi,

    Based on your previous post, I think you want to convert the special keys to win32 virtual key codes.

    I am not quite sure what's the original type of the "special key". In a WPF application, you might catch it in a keyboard event as System.Windows.Input.Key. Then you can use KeyInterop.VirtualKeyFromKey method to convert it to a virtual key code.

    If the "special key" is string, you can try Enum class's TryParse or Parse method to convert the string to a WPF key, and use the above method to further convert it to a virtual key code.

    If the "special key" is winform key, you can simply use Convert.ToUInt32 to convert it to a virtual key code.

    Hope this helps.


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by gidi Wednesday, March 23, 2011 9:42 AM
    Wednesday, March 23, 2011 3:10 AM
    Moderator
  • If you capture the Key value from an event, check the it against keys.System. If its a system key, simply get the SystemKey, like so:

    (the variable key will have the value you are looking for and you can do what you want with it)

     

     private static void onKeyDown(object sender, KeyEventArgs e)
            {
                UIElement element = sender as UIElement;
     
                if (null != element)
                {
                    Key key = e.Key; 
     
                    if (e.Key == Key.System)
                    {
                        key = e.SystemKey; 
                    }
                }
            }
    Wednesday, January 04, 2012 7:02 PM