Page 1 of 1

Detect Keypress on Launch ?

Posted: Sat Oct 27, 2012 10:49 am
by grabiller
Hi,

Following my post from the Coding Question forum:
http://www.purebasic.fr/english/viewtop ... 13&t=51755

I have it working on Windows, and, thanks to wilbert, I also have it working on Mac OSX now.

Now I'm trying to have it working on Linux as well :wink:

Any idea how would I detect keypress (I'm only interested in modifiers, mostly SHIFT actually, but..) on program launch ?

If I wait until I open a window and monitor the events, the keypress is long gone. I'd like to know, for instance, is the user has pressed the SHIFT key while double-clicking on a program to launch it.

(as for the 'why', I need this to detect if the user wants to run my application in 'edit' mode rather than in 'run' mode, it's a simple way for the user to choose instead of using arguments, etc..)

Re: Detect Keypress on Launch ?

Posted: Sun Oct 28, 2012 2:02 pm
by grabiller
I've tried with this code which should work:

Code: Select all

      #_IOC_NRBITS   = 8
      #_IOC_TYPEBITS = 8
      #_IOC_SIZEBITS = 14
      #_IOC_DIRBITS  = 2
      ;
      #_IOC_NRSHIFT   =  0
      #_IOC_TYPESHIFT =  8 ; _IOC_NRSHIFT   + _IOC_NRBITS
      #_IOC_SIZESHIFT = 16 ; _IOC_TYPESHIFT + _IOC_TYPEBITS
      #_IOC_DIRSHIFT  = 30 ; _IOC_SIZESHIFT + _IOC_SIZEBITS
      ;
      #_IOC_READ = 2
      Macro _IOC(dir,type,nr,size)
        ( dir << #_IOC_DIRSHIFT ) | ( type << #_IOC_TYPESHIFT ) | ( nr << #_IOC_NRSHIFT ) | ( size << #_IOC_SIZESHIFT )
      EndMacro
      Macro EVIOCGKEY(len_)
        _IOC(#_IOC_READ,'E',$18,len_)
      EndMacro
      #KEY_MAX = $1FF
      #KEY_LEFTSHIFT  = 42
      #KEY_RIGHTSHIFT = 54
      Structure key_map_t
        k.b[#KEY_MAX/8+1]
      EndStructure
      Define current_dir = ExamineDirectory( #PB_Any, "/dev/input/by-path", "*-event-kbd" )
      If current_dir
        While NextDirectoryEntry( current_dir )
          If DirectoryEntryType( current_dir ) = #PB_DirectoryEntry_File
            Define fname$ = DirectoryEntryName( current_dir )
            #O_RDONLY   = 0
            #O_NONBLOCK = $4000
            Define *fd = open_("/dev/input/by-path/"+fname$,#O_RDONLY|#O_NONBLOCK,0)
            Debug *fd
            If *fd
              Define key_map.key_map_t
              Debug ioctl_(*fd,EVIOCGKEY(SizeOf(key_map_t)),@key_map)
              
              ;Debug "Dumping key_map:"
              ;Define i = 0, istop = #KEY_MAX/8+1
              ;While i < istop
                ;Debug key_map\k[i]
                ;i + 1
              ;Wend
              ;Debug "key_map dumped."
              
              ;Debug ( key_map\k[#KEY_LEFTSHIFT/8] & (1 << (#KEY_LEFTSHIFT%8)) )
              ;Debug ( key_map\k[#KEY_RIGHTSHIFT/8] & (1 << (#KEY_RIGHTSHIFT%8)) )
              
              close_(*fd)
            EndIf
          EndIf
        Wend
        FinishDirectory( current_dir )
but open returns -1 (error).

Error number is available from C in errno, but I don't know how to access errno from PB, I'm not sure it's even possible.

(fopen succeed, but then ioctl returns -1(error) as well)

No luck with Linux so far : -/