Michael T
Michael T

Reputation: 729

GetKeyNameText returns name in uppercase

I have a TextBox which is displaying the key name for individual keys - to display a hotkey combination. For example "Alt + Ctrl + T".

I have the following method which is called from PreviewKeyDown on a TextBox.

The string returned is the correct name for the key. The problem is when I press Ctrl + Shift this seems to toggle the case of the returned string from GetKeyNameText (Interestingly I have tried entering Ctrl + Shift keys on other third party apps that let you define hotkeys and they have the same issue.

public string KeyToString(Key key)
{
    StringBuilder sbString = new StringBuilder(256);

    uint virtualKeyCode = (uint)KeyInterop.VirtualKeyFromKey(key);

    // Gets the layout of keyboard
    IntPtr keyboardLayout = GetKeyboardLayout(0);

    // Map the virtual keycode
    uint scanCode = MapVirtualKeyEx(virtualKeyCode, 0, keyboardLayout);

    // Get Text
    GetKeyNameText(scanCode << 16 | (1 << 25), sbString, sbString.Capacity);

    return sbString.ToString(); ;
}

Upvotes: 0

Views: 573

Answers (1)

Michael T
Michael T

Reputation: 729

Figured it out - thanks.

I have different keyboard layouts loaded (ENG-US and ENG-INT) and I noticed that Ctrl + Shift toggles which layout I'm using - ENG-INT has the key names in upper case.

The only reference to this I found is here disable ctrl shift for layout

Upvotes: 2

Related Questions