Detect Keypress on Launch ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Detect Keypress on Launch ?

Post 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.
guy rabiller | radfac founder / ceo | raafal.org
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Detect Keypress on Launch ?

Post 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
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: Detect Keypress on Launch ?

Post by grabiller »

Thanks wilbert.

This works perfectly well.

Cheers.
guy rabiller | radfac founder / ceo | raafal.org
Post Reply