Main Site Documentation

USB Keyboard - values


#1

Hello all,

I was looking at this ASCII table:

http://ascii-table.com/

But what I see is that the button numbers returned from the keyboard are not ASCII, they are numerical:

Is there a way to transfer this to ascii? Or should I do this the quick and dirty way by using 2 arrays and comparing them?

example: USB keyboard returned 4, compare number array with string array:


static string AsciiToChar(int input)
{
    int[] number = {4,5,6,7,8);
    string[] strings = {a,b,c,d);

    string output = "";

    for (int i = 0; i < numbers.Length; i++)
    {
        if((string.Compare(input.ToString(), numbers[i].ToString())) == 0)
        {
             output = strings[i];
        }
    }  
          
    return output;
}

I hope there is a less dirty way? :stuck_out_tongue:

NOTE: there might be some errors in code, I am very tired. I guess you will know what I meant…


#2

Keyboards return key scan codes with have nothing to do with ASCII :frowning:

http://www.win.tue.nl/~aeb/linux/kbd/scancodes-14.html


#3

This is explained in documentation :slight_smile:


#4

This is for USB client, not USB host…?


#5

They work the same…
USB Host, we already provide an event which gives characters.
USB Client, we do not provide this. I am not seeing good use for it, do you have an example on why it is needed? Because the way I see it is you will have an interrupt input for a button for example, then you will do something like:
void myPinInterrupt()
{
Send(Key.A);
}
…etc

Transfer from keys to chars is not straightforward but it is not complicated either. For example, you have to keep track and account for Caps lock, shit key, num lock and provide correct numbers vs capital vs small letters…etc


#6

Wait wait, I guess we have a misunderstanding here.
I meant that the example in the ebook gives an example of using the usb client function instead of the usb host function which i would like to use.

Ebook:
Usb client
28.4 keyboard page 140.

The example shows the other way round (sending keys instead of reading and converting)


USBC_Keyboard kb = USBClientController.StandardDevices.StartKeyboard();

// Check if connected to PC
if (USBClientController.GetState() ==
USBClientController.State.Running)
{
    // We need shift down for capital "H"
    kb.KeyDown(USBC_Key.LeftShift);
    kb.KeyTap(USBC_Key.H);
    kb.KeyUp(USBC_Key.LeftShift);
    // Now "ello world"
    kb.KeyTap(USBC_Key.E);
    kb.KeyTap(USBC_Key.L);
    kb.KeyTap(USBC_Key.L);
    kb.KeyTap(USBC_Key.O);
    kb.KeyTap(USBC_Key.Space);
    kb.KeyTap(USBC_Key.W);
    kb.KeyTap(USBC_Key.O);
    kb.KeyTap(USBC_Key.R);
    kb.KeyTap(USBC_Key.L);
    kb.KeyTap(USBC_Key.D);
    // The "!"
    kb.KeyDown(USBC_Key.LeftShift);
    kb.KeyTap(USBC_Key.D1);
    kb.KeyUp(USBC_Key.LeftShift);
    // Send an enter key
    kb.KeyTap(USBC_Key.Enter);
}

#7

Yes, this is what I tried to explain. In real application why send ASCII values…


#8

I did not knew there was a ASCII outputter in the eventargs?
This solved my problem, thanks!


static void KeyboardDown(USBH_Keyboard sender, USBH_KeyboardEventArgs args)
        {
            Keyboard.ButtonDown = args.KeyAscii.ToString();
        }

        static void KeyboardUp(USBH_Keyboard sender, USBH_KeyboardEventArgs args)
        {
            Keyboard.ButtonUp = args.KeyAscii.ToString();
        }