[bb] Block Intrusive keys - StickyKey_dll by BlackJumper [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:41

Previous topic - Next topic

BlitzBot

Title : Block Intrusive keys - StickyKey_dll
Author : BlackJumper
Posted : 1+ years ago

Description : Disabling StickyKey shortcut ... and other Keyboard Intrusions
==============================================================
BlackJumper July 2005 (update from March 2005 posting)

<div class="quote"> This is an update to my StickyKeys_dll that I created to deal with the problem of Accessibility features in Windows that can intrude on a game that uses the Shift key (in particular) or the NUM Lock key. There were a number of requests on the BlitzBasic B3D board asking for ways to disable other intrusions such as accidentally hitting the Windows (Start) key or crashes caused by users ALT+TABbing within a game.

Features:
=========
1. Can toggle the Accessibility features (FilterKeys, StickyKeys, ToggleKeys) on and off
2. Can independently toggle ToggleKeys on/off
3. Can enable/disable SysKey combinations {Alt+TAB, Alt+Esc, Ctrl+Esc, LeftWIN key, RightWIN key} through simple toggle to restrict task switching from within game
4. Can toggle audible feedback for blocked SysKeys
5. Simple toggle to allow/deny any keyboard intrusions (put outside start + end of your game loop)

N.B. there is presently no Ctr+Alt+Delete blocking functionality
</div>

{original description}
After a night of tweaking my GUI for a game I realised that I had used the Shift key as a 'fire button' and the first time I tried to play it I got the Windows StickyKeys popup... aaargh!

Shambler put me on to some information on the Microsoft site that forms the core of the included dll. Of the three solutions suggested, 2 needed to be wrapped around the main game loop - which looked like a problem from Blitz, and the third made changes to a Control Panel.

Getting the third working is easy, but allowing for the fact that the program might crash and not restore original settings is a bit more problematic.
The dll creates a text file called "StickyKeys.txt" when you call SetStickyKeysState(True).
It then sets the file to 'Hidden' when you call SetStickyKeysState(False) and restores the original settings for the control panel.
If the file is visible, the dll will not overwrite it. The idea is that an unexpected crash will leave a visible file, to be used to restore settings next time the program is run.
<div class="quote">
Get the <a href="http://www.blackjumper.com/selling/v2_StickyKey_Lib.zip" target="_blank"> latest version - V2 zip file here</a>
</div>

......... if you have problems then you can .... Get the <a href="http://www.blackjumper.com/selling/v1_StickyKey_Lib.zip" target="_blank">Version 1 zip file here</a>

Put the .decls file and the StickyKey_dll.dll files in your userlibs folder in the Blitz folder.
The dll has six exported functions (originally first three...):

(1) SetStickyKeysState(boolState%) - set to True to allow sticky keys shortcuts** and False to disable them
(2) GetStickyKeysVersionNumber$() - returns version info for this dll
(3) SetToggleKeyToneState(boolState%) - set to True to have beeps for Caps Lock, etc. False to disable
(4) SetKeyboardTrap(boolTrapStatus%, boolBeep% ) - use to enable/disable the Keyboard Hook for Task Switching keys
(5) GetSystemKeysDisabledStatus() - check if hook is active
(6) KeyboardGameLock(boolLockDown%) - simple function to switch on/off all intrusive keyboard blocking

[edit]
** N.B. This will also turn off Filter Keys{controls keyboard repeat rate} and Toggle Keys{beeps for Caps Lock, Num Lock and Scroll Lock} flags.

N.B.2: I don't know exactly which versions of Windows these will run on. The SystemParametersInfo details on Microsoft's site has no disclaimers, so I would expect it to work from W95 onwards. I have tested on W2K and WinXP. Please post a reply to this thread if you get success on other versions of Windows.
[/edit]

You should probably use it like this:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     SetStickyKeysState(False)

     Repeat

          .... main game code that uses Shift key

     Until GameEnd

     SetStickyKeysState(True)

     End
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
e.g.

Graphics 640, 480
StickyState = 0
vernum$ = GetStickyKeysVersionNumber()

SetStickyKeysState(False)
SetToggleKeyToneState(False)
tones  = 0

Repeat
Text 20,20, "Keypressed " + GetKey()
Text 20,60, vernum
Text 20,100, "Stickystate is " + StickyState + "      ToggleKeyState is : " + tones

If KeyHit(31) Then
   StickyState = 1 - StickyState  
   SetStickyKeysState(StickyState)
EndIf

If KeyHit(20) Then
   tones = 1 - tones  
   SetToggleKeyToneState(tones)
EndIf  

Flip
Delay 300
Cls
Until KeyHit(1)
   
SetStickyKeysState(True)

see the sample program [/i]

Code :
Code (blitzbasic) Select
; New Version 2 code
; Locks down your game with a single function call
; July 2005
Graphics 640, 480

StopIntrusions = True
vernum$ = GetStickyKeysVersionNumber()

KeyboardGameLock(True)

Repeat
Text 20,0,  "'T' to toggle Keyboard intrusion features on/off ......."
Text 20,60, "Keypressed " + GetKey()
Text 20,100, vernum
Text 20,140, "Keyboard intrusion state is " + StopIntrusions

Text 20, 300, "try pressing SHIFT 5+ times (... can launch StickyKeys dialog);"
Text 20, 320, "hold down RightSHIFT For 8+ seconds (can launch FilterKeys dialog);"
Text 20, 340, "hold down NUMLOCK for 5+ seconds (can launch ToggleKeys dialog);"
Text 20, 360, "use ALT+TAB, ALT+Esc, CTRL+Esc Or the Win key ... can cause task switching"


If KeyHit(20) Then                 ;  'T' to toggle StopIntrusions
   StopIntrusions = 1 - StopIntrusions
   KeyboardGameLock( StopIntrusions )
EndIf
 

Flip False
Delay 30
Cls
Until KeyHit(1)
   
KeyboardGameLock(False) ; ESSENTIAL - Unhook GLOBAL (all apps) filter

End


Comments :


Neochrome(Posted 1+ years ago)

 THIS is a answered Prayer!!trust Micro$oft to make like more annoying for us!


BlackJumper(Posted 1+ years ago)

 Updated to version 2 (adds Low Level keyboard hook to trap Alt+TAB, WIN key, etc.)


Grey Alien(Posted 1+ years ago)

 Nice one!


Grey Alien(Posted 1+ years ago)

 OK did some 98 SE testing.  It certainly doesn't crash or cause any errors, but it doesn't prevent any keys at all.  I tested Windows Key, Alt+Tab, Ctrl+Alt+Del, Windows+R.


big10p(Posted 1+ years ago)

 Gerry's already fully aware of this not working on win98, much to his chagrin. :) I did some testing for him, back along.


_33(Posted 1+ years ago)

 Any way to avoid generating a StickyKey.txt file?  It looks mostly like a debug file, and seves, to me, absolutely no purpose, and might pose more confusion than anything.If StickyKeys need a workspace to store it's backup data, I'd prefer to use a bank and shuffle the data in there, much much cleaner that way.Cheers.