I am using a G130E Dev Board 1.2
using GHI.Usb.Host Controller
//Outside of Main()
Controller.DeviceConnectFailed += Controller_DeviceConnectFailed;
Controller.KeyboardConnected += Controller_KeyboardConnected;
Controller.Start();
// I do not get key press events unless I use the Reset() ?
// I can use Reset either before or after Controller.Start()
// and I get the same results;
Controller.Reset();
private static void Controller_KeyboardConnected(object sender, Keyboard keyboard)
{
keyboard.Disconnected += keyboard_Disconnected;
//Key press events
keyboard.KeyDown += keyboard_KeyDown;
keyboard.KeyUp += keyboard_KeyUp;
}
//
I do not receive events
keyboard_KeyDown or keyboard_KeyUp
unless I use Controller.Reset();
I had no idea this was necessary.
Does anyone know why?
1 Like
@ willgeorge -
Are you using G120E?
I am not sure because you didn’t push all code. Here is my simple code and tested on 3 keyboards: Accer, Microsoft, Logitech K120. they all working well.
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHI.Usb;
using GHI.Usb.Host;
using GHI.Pins;
namespace TestUSBHost_keyboard
{
public class Program
{
static OutputPort led = new OutputPort(G120E.Gpio.P3_27, true);
static InputPort button = new InputPort(G120E.Gpio.P0_22, false, Port.ResistorMode.PullUp);
public static void InitUSBKeyboard()
{
Controller.DeviceConnectFailed += Controller_DeviceConnectFailed;
Controller.KeyboardConnected += Controller_KeyboardConnected;
Controller.Start();
}
static void Controller_DeviceConnectFailed(object sender, EventArgs e)
{
Debug.Print("Device connected failed!");
}
public static void Main()
{
while (button.Read() == true)
{
Thread.Sleep(100);
led.Write(!led.Read());
}
InitUSBKeyboard();
Debug.Print("USB Host inited");
Thread.Sleep(-1);
}
private static void Controller_KeyboardConnected(object sender, Keyboard keyboard)
{
Debug.Print("Device connected!");
keyboard.Disconnected += keyboard_Disconnected;
//Key press events
keyboard.KeyDown += keyboard_KeyDown;
keyboard.KeyUp += keyboard_KeyUp;
}
static void keyboard_KeyUp(Keyboard sender, Keyboard.KeyboardEventArgs args)
{
Debug.Print("Up: " + args.ASCII);
}
static void keyboard_KeyDown(Keyboard sender, Keyboard.KeyboardEventArgs args)
{
Debug.Print( "Down: " + args.ASCII);
}
static void keyboard_Disconnected(BaseDevice sender, EventArgs e)
{
Debug.Print("Device disconnected.");
}
}
}
@ Dat -
Thanks much… I will try your code and see what happens.
(Though I cannot do it today)
I was using a G120E Dev Board 1.2
The keyboards worked but I only got the key press events if I used the reset first.
@ willgeorge -
Problem is in my code, I didn’t use reset function.