Jump to content
davornik

Intercept keys from hardware keyboard (HID Device)

Recommended Posts

Is it posible in Android to intercept keyboard presses from hardware keyboard attached to device? I can detect is HID Device present, but how to get key input from HID before form does?

function TForm1.AttachedHIDDevice: Boolean;
var
  i: Jiterator;
  JavaObject: JObject;
  DeviceList: JHashMap;
  USBDevice: JUSBDevice;
  UsbManager: JUSBManager;
begin
  //Device discovery
  Result := False;
  //Get pointer to UsbManager
  JavaObject := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.USB_SERVICE);
  UsbManager := TJUsbManager.Wrap((JavaObject as ILocalObject).GetObjectID);
  if not Assigned(UsbManager) then Exit;
  //Get a list of connected devices
  DeviceList := UsbManager.getDeviceList;
  i := DeviceList.values.iterator;
  while i.hasNext do begin
    USBDevice := TJUsbDevice.Wrap((i.next as ILocalObject).GetObjectID);
    if (USBDevice.getInterfaceCount > 0) then
      begin
        Result := USBDevice.getInterface(0).getInterfaceClass = TJUsbConstantsUSB_CLASS_HID;
        if Result then Break;
      end;
  end;
end;

 

Share this post


Link to post
Guest

maybe you would have use the "object -->form, edit, etc..." receiving input, not necessary the usbDevice connected.

Share this post


Link to post

That is not good solution, has some issues. However, OnFormKeyUp is better choice (key is possible to receive only once according to Android docs), but if TEdit is focused TForm doesn't get key.

Then, again, if I type Key in external keyboard and I am in TEdit control, key is shown in TEdit (I would like to Intercept it before it reaches any Control on Form or Form itself).

 

Is it possible to differentiate Key origin?

Is it received from Virtual Keyboard or HID device?

Edited by davornik

Share this post


Link to post

I have no external keyboard, how does it behave differently ?

Isn't it catched by the normal TEdits OnKeyDown, OnKeyUp or OnChangeTracking in same way as should with the virtual keyboard ?

Share this post


Link to post

I would like to intercept Key from external keyboard before it reaches TEdit or Form. Is this possible?

Share this post


Link to post

Why ? If you want to igore a key you could abort operation, and also do other oprtation.

Do you want to determine where the key is sended from, to do differentiate operations depending on sended from hardware or virtual keyboard ?

Share this post


Link to post

idk, i thought being fmx and no KeyPreview.

looks like you override the method KeyDown of the form, goes there first then out as IsDialogKey or something like that.

but i don't think you can tell which keyboard it comes from, you need to access the device directly using a provided api or something.

Share this post


Link to post
On 2/25/2022 at 6:10 PM, Rollo62 said:

Why ? If you want to igore a key you could abort operation, and also do other oprtation.

Do you want to determine where the key is sended from, to do differentiate operations depending on sended from hardware or virtual keyboard ?

Yes, I want to determine from where key is sent.

Share this post


Link to post

Ok, in that case maybe this can be helpful, but its not a complete Delphi solution yet, you have to look into the sources.

Quote

When the user gives focus to an editable text view such as an EditText element and the user has a hardware keyboard attached, all input is handled by the system. If, however, you'd like to intercept or directly handle the keyboard input yourself, you can do so by implementing callback methods from the KeyEvent.Callback interface, such as onKeyDown() and onKeyMultiple().

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×