Page 1 of 1

How obtain which keyboard is used

Posted: Thu Jun 04, 2020 1:12 pm
by Klonk
Hi everyone,

maybe it was asked somewhere else, but at least I haven't found anything.

I want to create a tool (basically I need it myself) which temporary "locks" the internal keyboard of a laptop without interfering with an external one.

I thought of doing it that way:
* Check on which keyboard (internal or external) a key was pressed.
* if internal keyboard has been used empty keyboard buffer. Thus no key is handed over to any other application
* if external keyboard was used hand over key to system for other applications.

For doing this I need some stuff which I haven't found good information on:
1. How to find out which keyboard was pressed?
2. How to hook into the system so all key pressed can be checked?
3. How to hand over a pressed key to the system again?

I guess I have to use WinAPI as it would not work with builtin functions of purebasic, right?

BTW: I want to write it for Windows

Any ideas or hints?

P.S. I know I could do the same by deinstalling the keyboard driver. On the next reboot the driver would be installed again. But I don't like the need to reboot the computer

Many thnaks

Re: How obtain which keyboard is used

Posted: Thu Jun 04, 2020 1:47 pm
by Mijikai
Should be doable with RawInput (https://docs.microsoft.com/de-de/window ... /raw-input)

Enumerate the all available input devices with GetRawInputDeviceList().
If the device is a Keyboard u can get more info about it with GetRawInputDeviceInfo().
If using RawInput - an input event is sent with the Device Handle to indentify the Device (Keyboard).

Note: I havent looked into intercepting or disabling keyboards!

I tried to get the first step working but i could not get it to work :S

Anyway heres what i tried maybe someone can help:

Code: Select all

EnableExplicit

Import "kernel32.lib"
  GetProcAddress.i(hModule.i,ProcName.p-ascii)
EndImport

Procedure.i GetApi(Library.s,ProcName.s)
  ProcedureReturn GetProcAddress(GetModuleHandle_(Library),ProcName)
EndProcedure

Procedure.i Main()
  Protected *GetRawInputDeviceList;<- API not supported by PB but Structurs !? :(
  Protected devices.i
  Protected *buffer
  Protected *device.RAWINPUTDEVICELIST
  Protected index.i
  *GetRawInputDeviceList = GetApi("user32","GetRawInputDeviceList")
  If *GetRawInputDeviceList
    CallFunctionFast(*GetRawInputDeviceList,#Null,@devices,SizeOf(RAWINPUTDEVICELIST))
    If devices
      *buffer = AllocateMemory(devices * SizeOf(RAWINPUTDEVICELIST))
      If *buffer
        If Not CallCFunctionFast(*buffer,@devices,SizeOf(RAWINPUTDEVICELIST)) = -1
          *device = *buffer
          For index = 1 To devices
            If *device\dwType = #RIM_TYPEKEYBOARD
              Debug "Keyboard: " + *device\hDevice
            EndIf
            *device + SizeOf(RAWINPUTDEVICELIST)
          Next
        EndIf
        FreeMemory(*buffer)
      EndIf
    EndIf
  EndIf
  ProcedureReturn #Null
EndProcedure

Main()

End
Maybe theres something wrong with the structure - not sure!
Good luck!

Re: How obtain which keyboard is used

Posted: Tue Jun 09, 2020 1:58 pm
by Klonk
Many thanks for that. Haven't tried it yet though.

I think the main problem is that in order to make my plans work the keyboard input has to be intercepted at a very early stage which
1. need to install a driver of some sort for intercepting (maybe library "interception" may work for that: https://github.com/oblitum/Interception)
2. such an early interception might be recognized as virus.

I think using interception might be the easiest way. I have to take a closer look to the library and it's functions.

Re: How obtain which keyboard is used

Posted: Tue Jun 09, 2020 2:03 pm
by Derren
Look for Keyboard Hook to catch the key presses. You should be able to stop them from reaching focused programm.
If you can combine it with what Mijikai said, you might have yourself a nice solution.

Re: How obtain which keyboard is used

Posted: Tue Jun 09, 2020 2:58 pm
by ZX80
Klonk, there is related topic. The same problem. Just remove the structures if you are using the latest version of PB (already declared, built-in).