Page 1 of 1

Detect Keypress on Launch ?

Posted: Tue Oct 16, 2012 12:26 pm
by grabiller
Hi,

Just to be sure I did not overlooked something:

Is there a native PureBasic way of detecting key pressed on executable launch ?

Say I want to know if one of the SHIFT keys is pressed when the user launch the application.

I can't use ExamineKeyboard and the like as this require opening a screen. I want to detect keys even with Console applications (yet I'm not sure ExamineKeyboard would detect keys on application launch).

I'm well aware of the Windows API GetAsyncKeyState() which answers my question.

So if there is no native PureBasic way of doing this, what would be the equivalent of GetAsyncKeyState() on Linux and Mac OSX ?

Thanks.

Re: Detect Keypress on Launch ?

Posted: Tue Oct 16, 2012 2:15 pm
by wilbert
OS X - I'm not familiar with console applications so you would have to check if it works there also

Code: Select all

#NSAlphaShiftKeyMask = 1 << 16
#NSShiftKeyMask      = 1 << 17
#NSControlKeyMask    = 1 << 18
#NSAlternateKeyMask  = 1 << 19
#NSCommandKeyMask    = 1 << 20

ImportC "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"
  CGEventCreate(source)
  CGEventGetFlags(event)
  CFRelease(cf)
EndImport

event = CGEventCreate(#Null)
modifiers = CGEventGetFlags(event)
CFRelease(event)

If modifiers & #NSShiftKeyMask
  ; started with shift pressed
EndIf

Re: Detect Keypress on Launch ?

Posted: Tue Oct 16, 2012 8:28 pm
by grabiller
Thanks wilbert.

This works perfectly well.

Cheers.