locked
VirtualKey - have to write a conversion class ?

    Question

  • Hello,

    my Problem is that i want to display the Name of a key a user pressed. In old Windows Forms this was very easy with this c# example ( works also in c++)

    private keys HotKey; private void txtbCaptureHotkey_KeyDown(object sender, KeyEventArgs e) {

    HotKey = e.KeyCode; txtboxCaptureKey.Text = e.KeyCode.ToString(); }

    as said this works also perfectly in c++ in Windows Forms. However if u try to do the same thing in Windows Metro u have to rely on the VirtualKey from Windows System and would do it like this

    Windows::System::VirtualKey HotKey;

    void TestClass::MainPage::txtboxCaptureKey_KeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e) { HotKey = e->Key; HotKeyValue = (int)e->Key; txtboxCaptureKey->Text = HotKey.ToString(); // i could use the Value from the enumeration but i Need to Display the keyname }

    This will produce "Windows::System::VirtualKey" as Text in the CaptureKey Textbox. When i searched for this in internet past week i stumbled over the "Create a Blog Reader Tutorial" on msdn as search result. In somewhat the middle of the Tutorial it has the same problem with the date time instead and theres momentarely no automatic conversion for this , so the Tutorial shows how to write a own conversion class. But do i have to write a own conversion class with somewhat 15-20 lines only to show the key i already got as Value ?

    I think it would make sense for Microsoft to implement a conversion or a 2nd return value from the VirtualKey in the future like they had with Windows Forms

    PLease i appreciate any help, maybe someone has already found way to this

    greets co0Kie



    Monday, September 17, 2012 10:12 PM

Answers

  • Hi,

    I think there is no direct way to do this.
    We can create a string array contains all the virtual key member with its index.

    For example we can set PageDown in the array 34. When we get the e-Key, we can call array->GetAt(e->Key), then return a string.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Francis Grave Wednesday, September 19, 2012 2:40 PM
    Wednesday, September 19, 2012 11:38 AM
  • Hi,

    i solved my problem yesterday by writing a seperate header which has a swicth, but your idea of an array is very nice and would work too. Maybe i will give it a try sometime, but have to much things stucking here with :)

    Even though my solution may be not as fast as Jesses, heres my header for anybody who needs the native description of a key in c++ Metro Store App. Just add a new header element to your Project and copy&past this :

    #pragma once using namespace Platform; namespace Keys { inline String^ getKey(int KeyValue) { String^ keyString; switch(KeyValue) { case 0: break; case 1: keyString = "Mouse Left"; break; case 2: keyString = "Mouse Right"; break; case 3: keyString = "Cancel"; break; case 4: keyString = "Mouse Middle"; break; case 5: keyString = "Special 1"; break; case 6: keyString = "Special 2"; break; case 8: keyString = "Back"; break; case 9: keyString = "TAB"; break; case 12: keyString = "Clear"; break; case 13: keyString = "Enter"; break; case 16: keyString = "Shift"; break; case 17: keyString = "Ctrl"; break; case 18: keyString = "Menu"; break; case 19: keyString = "Pause"; break; case 20: keyString = "Caps Lock"; break; case 21: keyString = "Kana/Hangul"; break; case 23: keyString = "Junja"; break; case 24: keyString = "Final"; break; case 25: keyString = "Hanja/Kanji"; break; case 27: keyString = "Esc"; break; case 28: keyString = "Convert"; break; case 29: keyString = "NonConvert"; break; case 30: keyString = "Accept"; break; case 31: keyString = "Mode"; break; case 32: keyString = "Space"; break; case 33: keyString = "Page Up"; break; case 34: keyString = "Page Down"; break; case 35: keyString = "End"; break; case 36: keyString = "Home"; break; case 37: keyString = "Left"; break; case 38: keyString = "Up"; break; case 39: keyString = "Right"; break; case 40: keyString = "Down"; break; case 41: keyString = "Select"; break; case 42: keyString = "Print"; break; case 43: keyString = "Execute"; break; case 44: keyString = "Snapshot"; break; case 45: keyString = "Insert"; break; case 46: keyString = "Delete"; break; case 47: keyString = "Help"; break; case 48: keyString = "Num 0"; break; case 49: keyString = "Num 1"; break; case 50: keyString = "Num 2"; break; case 51: keyString = "Num 3"; break; case 52: keyString = "Num 4"; break; case 53: keyString = "Num 5"; break; case 54: keyString = "Num 6"; break; case 55: keyString = "Num 7"; break; case 56: keyString = "Num 8"; break; case 57: keyString = "Num 9"; break; case 65: keyString = "A"; break; case 66: keyString = "B"; break; case 67: keyString = "C"; break; case 68: keyString = "D"; break; case 69: keyString = "E"; break; case 70: keyString = "F"; break; case 71: keyString = "G"; break; case 72: keyString = "H"; break; case 73: keyString = "I"; break; case 74: keyString = "J"; break; case 75: keyString = "K"; break; case 76: keyString = "L"; break; case 77: keyString = "M"; break; case 78: keyString = "N"; break; case 79: keyString = "O"; break; case 80: keyString = "P"; break; case 81: keyString = "Q"; break; case 82: keyString = "R"; break; case 83: keyString = "S"; break; case 84: keyString = "T"; break; case 85: keyString = "U"; break; case 86: keyString = "V"; break; case 87: keyString = "W"; break; case 88: keyString = "X"; break; case 89: keyString = "Y"; break; case 90: keyString = "Z"; break; case 91: keyString = "Windows Left"; break; case 92: keyString = "Windows Right"; break; case 93: keyString = "Application"; break; case 95: keyString = "Sleep"; break; case 96: keyString = "NumPad 0"; break; case 97: keyString = "NumPad 1"; break; case 98: keyString = "NumPad 2"; break; case 99: keyString = "NumPad 3"; break; case 100: keyString = "NumPad 4"; break; case 101: keyString = "NumPad 5"; break; case 102: keyString = "NumPad 6"; break; case 103: keyString = "NumPad 7"; break; case 104: keyString = "NumPad 8"; break; case 105: keyString = "NumPad 9"; break; case 106: keyString = "NumPad *"; break; case 107: keyString = "NumPad +"; break; case 108: keyString = "NumPad ."; break; case 109: keyString = "NumPad -"; break; case 110: keyString = "NumPad ,"; break; case 111: keyString = "NumPad /"; break; case 112: keyString = "F1"; break; case 113: keyString = "F2"; break; case 114: keyString = "F3"; break; case 115: keyString = "F4"; break; case 116: keyString = "F5"; break; case 117: keyString = "F6"; break; case 118: keyString = "F7"; break; case 119: keyString = "F8"; break; case 120: keyString = "F9"; break; case 121: keyString = "F10"; break; case 122: keyString = "F11"; break; case 123: keyString = "F12"; break; case 124: keyString = "F13"; break; case 125: keyString = "F14"; break; case 126: keyString = "F15"; break; case 127: keyString = "F16"; break; case 128: keyString = "F17"; break; case 129: keyString = "F18"; break; case 130: keyString = "F19"; break; case 131: keyString = "F20"; break; case 132: keyString = "F21"; break; case 133: keyString = "F22"; break; case 134: keyString = "F23"; break; case 135: keyString = "F24"; break; case 144: keyString = "Num lock"; break; case 145: keyString = "Scroll"; break; case 160: keyString = "Shift Left"; break; case 161: keyString = "Shift Right"; break; case 162: keyString = "Ctrl Left"; break; case 163: keyString = "Ctrl Right"; break; case 164: keyString = "Menu Left"; break; case 165: keyString = "Menu Right"; break; default: break; }

    return keyString; } }

    wfr = with friendly regards

    co0Kie

    • Marked as answer by Francis Grave Wednesday, September 19, 2012 2:40 PM
    Wednesday, September 19, 2012 2:35 PM

All replies

  • Hi,

    We need the virtual key convert to char, and there is no directly way to cover this.

    But the Virtualkey value is the same with ASCII value, so that we can cover the key value to char directly.
    char res=(char)e->Key;

    Here is the virtual key value document
    http://msdn.microsoft.com/en-us/library/windows/apps/windows.system.virtualkey

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, September 18, 2012 7:14 AM
  • Hi,

    thx for your effort, but it still produces the same like i described above. My Scenario here is the following :

    A User presses a key and the exact native description must be shown in a TextBox, Textblock or similar. That means if the user pressed the 5 on the NumPad my program gives me via a 1 line convert a string back which is " numpad 5 ".

    As i understand it from your post, you had "bigger" conversion code routines in the old Windows System for automaticly managing such conversions in background. Are these conversions are now abandoned to keep the OS slim and fast ( which win 8 is definitly) ? Your code example still gives me a 119 back for when i press F8 for example, but as said i would need the native description. That means, even if the user would press the Pause key or so, it should be possible to get the native description which would be "pause".

    wfr

    co0Kie



    Tuesday, September 18, 2012 10:10 AM
  • Hi,

    I think there is no direct way to do this.
    We can create a string array contains all the virtual key member with its index.

    For example we can set PageDown in the array 34. When we get the e-Key, we can call array->GetAt(e->Key), then return a string.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Francis Grave Wednesday, September 19, 2012 2:40 PM
    Wednesday, September 19, 2012 11:38 AM
  • Hi,

    i solved my problem yesterday by writing a seperate header which has a swicth, but your idea of an array is very nice and would work too. Maybe i will give it a try sometime, but have to much things stucking here with :)

    Even though my solution may be not as fast as Jesses, heres my header for anybody who needs the native description of a key in c++ Metro Store App. Just add a new header element to your Project and copy&past this :

    #pragma once using namespace Platform; namespace Keys { inline String^ getKey(int KeyValue) { String^ keyString; switch(KeyValue) { case 0: break; case 1: keyString = "Mouse Left"; break; case 2: keyString = "Mouse Right"; break; case 3: keyString = "Cancel"; break; case 4: keyString = "Mouse Middle"; break; case 5: keyString = "Special 1"; break; case 6: keyString = "Special 2"; break; case 8: keyString = "Back"; break; case 9: keyString = "TAB"; break; case 12: keyString = "Clear"; break; case 13: keyString = "Enter"; break; case 16: keyString = "Shift"; break; case 17: keyString = "Ctrl"; break; case 18: keyString = "Menu"; break; case 19: keyString = "Pause"; break; case 20: keyString = "Caps Lock"; break; case 21: keyString = "Kana/Hangul"; break; case 23: keyString = "Junja"; break; case 24: keyString = "Final"; break; case 25: keyString = "Hanja/Kanji"; break; case 27: keyString = "Esc"; break; case 28: keyString = "Convert"; break; case 29: keyString = "NonConvert"; break; case 30: keyString = "Accept"; break; case 31: keyString = "Mode"; break; case 32: keyString = "Space"; break; case 33: keyString = "Page Up"; break; case 34: keyString = "Page Down"; break; case 35: keyString = "End"; break; case 36: keyString = "Home"; break; case 37: keyString = "Left"; break; case 38: keyString = "Up"; break; case 39: keyString = "Right"; break; case 40: keyString = "Down"; break; case 41: keyString = "Select"; break; case 42: keyString = "Print"; break; case 43: keyString = "Execute"; break; case 44: keyString = "Snapshot"; break; case 45: keyString = "Insert"; break; case 46: keyString = "Delete"; break; case 47: keyString = "Help"; break; case 48: keyString = "Num 0"; break; case 49: keyString = "Num 1"; break; case 50: keyString = "Num 2"; break; case 51: keyString = "Num 3"; break; case 52: keyString = "Num 4"; break; case 53: keyString = "Num 5"; break; case 54: keyString = "Num 6"; break; case 55: keyString = "Num 7"; break; case 56: keyString = "Num 8"; break; case 57: keyString = "Num 9"; break; case 65: keyString = "A"; break; case 66: keyString = "B"; break; case 67: keyString = "C"; break; case 68: keyString = "D"; break; case 69: keyString = "E"; break; case 70: keyString = "F"; break; case 71: keyString = "G"; break; case 72: keyString = "H"; break; case 73: keyString = "I"; break; case 74: keyString = "J"; break; case 75: keyString = "K"; break; case 76: keyString = "L"; break; case 77: keyString = "M"; break; case 78: keyString = "N"; break; case 79: keyString = "O"; break; case 80: keyString = "P"; break; case 81: keyString = "Q"; break; case 82: keyString = "R"; break; case 83: keyString = "S"; break; case 84: keyString = "T"; break; case 85: keyString = "U"; break; case 86: keyString = "V"; break; case 87: keyString = "W"; break; case 88: keyString = "X"; break; case 89: keyString = "Y"; break; case 90: keyString = "Z"; break; case 91: keyString = "Windows Left"; break; case 92: keyString = "Windows Right"; break; case 93: keyString = "Application"; break; case 95: keyString = "Sleep"; break; case 96: keyString = "NumPad 0"; break; case 97: keyString = "NumPad 1"; break; case 98: keyString = "NumPad 2"; break; case 99: keyString = "NumPad 3"; break; case 100: keyString = "NumPad 4"; break; case 101: keyString = "NumPad 5"; break; case 102: keyString = "NumPad 6"; break; case 103: keyString = "NumPad 7"; break; case 104: keyString = "NumPad 8"; break; case 105: keyString = "NumPad 9"; break; case 106: keyString = "NumPad *"; break; case 107: keyString = "NumPad +"; break; case 108: keyString = "NumPad ."; break; case 109: keyString = "NumPad -"; break; case 110: keyString = "NumPad ,"; break; case 111: keyString = "NumPad /"; break; case 112: keyString = "F1"; break; case 113: keyString = "F2"; break; case 114: keyString = "F3"; break; case 115: keyString = "F4"; break; case 116: keyString = "F5"; break; case 117: keyString = "F6"; break; case 118: keyString = "F7"; break; case 119: keyString = "F8"; break; case 120: keyString = "F9"; break; case 121: keyString = "F10"; break; case 122: keyString = "F11"; break; case 123: keyString = "F12"; break; case 124: keyString = "F13"; break; case 125: keyString = "F14"; break; case 126: keyString = "F15"; break; case 127: keyString = "F16"; break; case 128: keyString = "F17"; break; case 129: keyString = "F18"; break; case 130: keyString = "F19"; break; case 131: keyString = "F20"; break; case 132: keyString = "F21"; break; case 133: keyString = "F22"; break; case 134: keyString = "F23"; break; case 135: keyString = "F24"; break; case 144: keyString = "Num lock"; break; case 145: keyString = "Scroll"; break; case 160: keyString = "Shift Left"; break; case 161: keyString = "Shift Right"; break; case 162: keyString = "Ctrl Left"; break; case 163: keyString = "Ctrl Right"; break; case 164: keyString = "Menu Left"; break; case 165: keyString = "Menu Right"; break; default: break; }

    return keyString; } }

    wfr = with friendly regards

    co0Kie

    • Marked as answer by Francis Grave Wednesday, September 19, 2012 2:40 PM
    Wednesday, September 19, 2012 2:35 PM