[futurebasic] Re: [FB] GetCurrentKeyModifiers

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : June 2003 : Group Archive : Group : All Groups

From: Alain Pastor <apastor@...>
Date: Fri, 13 Jun 2003 15:21:38 +0200

Ken Shmidheiser wrote:
> In some recent experimentation, I discovered the GetCurrentKeyModifiers 
> function which appears very simple to use.
> 
> I have written the following code snippet to demonstrate what happens 
> when a single modifier key is pressed. However, when multiple modifier 
> keys are simultaneously pressed their bit values are automatically added 
> together and returned in a single integer by GetCurrentKeyModifiers.
> 
> My question is: How would one go about parsing out from the returned 
> integer just which modifier keys had been pressed? I guess I could build 
> an exhaustive select case table, but there must be an easier way.
> 
> GetCurrentKeyModifiers may have been discussed here before (I know 
> Robert P. has shown us how to use GetKeys) and if so, please excuse this 
> post. But my little peanut brain can't seem to recall this specific 
> function for modifiers.
> 
> I'm posting this because modifier key implementation is a recurrent 
> question to the list and this shows promise of an easy solution.
> 

Perhaps, you could do this:

toolbox fn GetCurrentKeyModifiers = UInt32

begin enum
_waiting      =    0
_cmdMod       =  256
_shiftMod     =  512
_alphaLockMod = 1024
_optionMod    = 2048
_controlMod   = 4096
end enum

dim as integer whichKey,oldKey : oldKey = -1
dim as rect    r

setrect( r, 0, 0, 200, 70 )
window 1, "Modifier Keys", @r
setrect( r, 20, 20, 180, 45 )
text _applFont,10
edit field 1, , @r, _statFramed, _centerJust

do
whichKey = fn GetCurrentKeyModifiers
long if oldKey != whichKey
oldKey = whichKey
if whichKey == _waiting       Then edit$(1) = "Waiting" else edit$(1) = ""
if whichKey and _cmdMod       then edit$(1,_maxInt,_maxInt) = "Comand 
key "
if whichKey and _shiftMod     then edit$(1,_maxInt,_maxInt) = "Shift key "
if whichKey and _alphaLockMod then edit$(1,_maxInt,_maxInt) = "Caps lock "
if whichKey and _optionMod    then edit$(1,_maxInt,_maxInt) = "Option 
key "
if whichKey and _controlMod   then edit$(1,_maxInt,_maxInt) = "Control 
key "
end if
handleevents
until fn button