How obtain which keyboard is used

Just starting out? Need help? Post your questions and find answers here.
Klonk
Enthusiast
Enthusiast
Posts: 173
Joined: Tue Jul 13, 2004 2:17 pm

How obtain which keyboard is used

Post 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
Bye Karl
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: How obtain which keyboard is used

Post 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!
Klonk
Enthusiast
Enthusiast
Posts: 173
Joined: Tue Jul 13, 2004 2:17 pm

Re: How obtain which keyboard is used

Post 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.
Bye Karl
User avatar
Derren
Enthusiast
Enthusiast
Posts: 313
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: How obtain which keyboard is used

Post 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.
ZX80
Enthusiast
Enthusiast
Posts: 331
Joined: Mon Dec 12, 2016 1:37 pm

Re: How obtain which keyboard is used

Post 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).
Post Reply