November 28, 2020, 02:24:01 PM

Author Topic: [bmx] Disable Sticky Keys (Windows) by Zethrax [ 1+ years ago ]  (Read 660 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Disable Sticky Keys (Windows)
Author : Zethrax
Posted : 1+ years ago

Description : *** BlitzMax code. See below for the link to the Blitz3D/BlitzPlus version. ***

Turns off the Windows OS Sticky Keys prompt and hotkeys.

Sticky keys is an annoying feature of the Windows operating system. To find out why it's a problem, tap your shift key five or more times in a row. If you use that key in your game's controls then the player won't be happy.

To disable Sticky Keys just run the 'TStickyKeys.Disable' function at the beginning of your program.

The Windows API flags that control the Sticky Keys prompt and hotkeys are automatically restored again when your program ends.

The TStickyKeys.Restore function will be called automatically by the OnEnd handler set in TStickyKeys.Disable.

Notes:-
   The functions will save and delete a temporary file named "stickykeys.dat". That file is used to restore the state of the sticky keys on the users system in case the program crashes before the 'TStickyKeys.Restore' function can be run (this seems to be best practice in this case). If files can't be saved to the folder the program is in then the 'filepath' global variable set in the 'TStickyKeys' type declaration should be changed to save to a folder that can be saved to.
   
You can find the Blitz3D (and probably BlitzPlus) version of this code at:-
   <a href="codearcs8bca.html?code=3003" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=3003[/url]

Reference links:-
   <a href="http://msdn.microsoft.com/en-us/library/ee416808(v=vs.85).aspx" target="_blank">http://msdn.microsoft.com/en-us/library/ee416808(v=vs.85).aspx[/url]
   <a href="http://msdn.microsoft.com/en-us/library/dd373652(v=vs.85).aspx" target="_blank">http://msdn.microsoft.com/en-us/library/dd373652(v=vs.85).aspx[/url]
   <a href="http://msdn.microsoft.com/en-us/library/ms724947(VS.85).aspx" target="_blank">http://msdn.microsoft.com/en-us/library/ms724947(VS.85).aspx[/url]


Code :
Code: BlitzMax
  1. ' Import this file into your program. Don't forget to remove the demo code at the bottom.
  2.  
  3. Strict
  4.  
  5. Import BRL.Blitz
  6. Import BRL.FileSystem
  7. Import BRL.Bank
  8.  
  9.  
  10. Extern "Win32"
  11.         Function SystemParametersInfoA:Int( uAction:Int, uParam:Int, lpvParam:Byte Ptr, fuWinIni:Int )
  12. End Extern
  13.  
  14.  
  15. Type TStickyKeys
  16.  
  17.         Global filepath:String = "stickykeys.dat"
  18.        
  19.         Global stickykeys_flags:Int
  20.        
  21.        
  22.         Function Disable()
  23.                 Rem
  24.                 Turns off the Sticky Keys prompt and hotkeys.
  25.                
  26.                 Sticky keys is an annoying feature of the Windows operating system. To find out why it's a problem, tap your
  27.                 shift key five or more times in a row. If you use that key in your game's controls then the player won't be happy.
  28.                
  29.                 To disable Sticky Keys just run this function at the begining of your program (eg. TStickyKeys.Disable).
  30.                
  31.                 The Windows API flags that control the Sticky Keys prompt and hotkeys are automatically restored again when your program ends.
  32.        
  33.                 The TStickyKeys.Restore function will be called automatically by the OnEnd handler set in TStickyKeys.Disable.
  34.                
  35.                 Notes:-
  36.                         The functions will save and delete a temporary file named "stickykeys.dat". That file is used to restore the state of the
  37.                         sticky keys on the users system in case the program crashes before the 'TStickyKeys.Restore' function can be run
  38.                         (this seems to be best practice in this case). If files can't be saved to the folder the program is in then the
  39.                         'filepath' global variable set above should be changed to save to a folder that can be saved to.
  40.                        
  41.                 You can find the Blitz3D (and probably BlitzPlus) version of this code at:-
  42.                         http://www.blitzbasic.com/codearcs/codearcs.php?code=3003
  43.                
  44.                 Reference links:-
  45.                         http://msdn.microsoft.com/en-us/library/ee416808(v=vs.85).aspx
  46.                         http://msdn.microsoft.com/en-us/library/dd373652(v=vs.85).aspx
  47.                         http://msdn.microsoft.com/en-us/library/ms724947(VS.85).aspx
  48.                 End Rem
  49.                
  50.                 Local SPI_GETSTICKYKEYS:Int = $0000003A
  51.                 Local SPI_SETSTICKYKEYS:Int = $0000003B
  52.                 Local SKF_HOTKEYACTIVE_MASK:Int = $00000004 ~ $ffffffff
  53.                 Local SKF_CONFIRMHOTKEY_MASK:Int = $00000008 ~ $ffffffff
  54.                
  55.                 ' Create a bank to store the STICKYKEYS structure.
  56.                 Local bank:TBank = CreateBank( 8 )
  57.        
  58.                 ' Set the first integer in the bank to the total size of the bank (8 bytes).
  59.                 PokeInt bank, 0, 8
  60.        
  61.                 ' Open the file that holds the STICKYKEYS flags.
  62.                 Local file:TStream = ReadFile( TStickyKeys.filepath )
  63.                
  64.                 If file ' If the file exists then the stickykeys state wasn't properly 
  65.                 ' restored last time - possibly due to a crash during the last session.
  66.                
  67.                         ' Grab the STICKYKEYS flags from the file so we can restore the state.
  68.                         PokeInt bank, 4, ReadInt( file )
  69.                         CloseFile file
  70.                        
  71.                 Else ' If the file doesn't exist then the stickykeys state was properly
  72.                 ' restored last time, so we get it via SystemParametersInfo.
  73.                        
  74.                         ' - Get the STICKYKEYS structure.
  75.                         ' First parameter is the value of the SPI_GETSTICKYKEYS Windows constant.
  76.                         ' Second parameter is the size of the STICKYKEYS structure again (8 bytes).
  77.                         ' Third parameter is the address of the STICKYKEYS structure (the bank pointer).
  78.                         ' Fourth parameter is not used and should be a zero.
  79.                         SystemParametersInfoA( SPI_GETSTICKYKEYS, 8, BankBuf( bank ), 0 )
  80.                        
  81.                 EndIf
  82.        
  83.                 ' Save the value of the second INT in the STICKYKEYS structure to a file.
  84.                 ' This allows the state to be restored in case a program crash happens before
  85.                 ' the 'RestoreStickyKeys' function can be run.
  86.                 file = WriteFile( TStickyKeys.filepath )
  87.                 If file
  88.                         WriteInt file, PeekInt( bank, 4 )
  89.                         CloseFile file
  90.                 EndIf
  91.        
  92.                 TStickyKeys.stickykeys_flags = PeekInt( bank, 4 )
  93.        
  94.                 ' Turn off the SKF_HOTKEYACTIVE and SKF_CONFIRMHOTKEY flags
  95.                 PokeInt bank, 4, ( TStickyKeys.stickykeys_flags & SKF_HOTKEYACTIVE_MASK ) & SKF_CONFIRMHOTKEY_MASK
  96.        
  97.                 ' Set the first integer in the bank to the total size of the bank (8 bytes).
  98.                 PokeInt bank, 0, 8
  99.        
  100.                 ' - Set the STICKYKEYS structure.
  101.                 ' First parameter is the value of the SPI_SETSTICKYKEYS Windows constant.
  102.                 ' Second parameter is the size of the STICKYKEYS structure again (8 bytes).
  103.                 ' Third parameter is the address of the STICKYKEYS structure (the bank pointer).
  104.                 ' Fourth parameter is not used and should be a zero.
  105.                 SystemParametersInfoA( SPI_SETSTICKYKEYS, 8, BankBuf( bank ), 0 )
  106.        
  107.                 'FreeBank bank - Doesn't seem to be an option to free a bank, so I guess we just let the GC clean it up.
  108.                
  109.                 OnEnd TStickyKeys.Restore ' Set the 'TStickyKeys.Restore' function to be called automatically when the program ends.
  110.         End Function
  111.        
  112.        
  113.         Function Restore()
  114.                 ' Restores the original state of the sticky keys prompt and hotkeys.
  115.                
  116.                 ' This function will be called automatically by the OnEnd handler set in TStickyKeys.Disable.
  117.        
  118.                 Local SPI_SETSTICKYKEYS:Int = $0000003B
  119.                
  120.                 ' Create a bank to store the STICKYKEYS structure.
  121.                 Local bank:TBank = CreateBank( 8 )
  122.                
  123.                 ' Set the first integer in the bank to the total size of the bank (8 bytes).
  124.                 PokeInt bank, 0, 8
  125.                
  126.                 ' Store the sticky keys flags in the second integer in the bank.
  127.                 PokeInt bank, 4, TStickyKeys.stickykeys_flags
  128.                
  129.                 ' - Set the STICKYKEYS structure.
  130.                 ' First parameter is the value of the SPI_SETSTICKYKEYS Windows constant.
  131.                 ' Second parameter is the size of the STICKYKEYS structure again (8 bytes).
  132.                 ' Third parameter is the address of the STICKYKEYS structure (the bank pointer).
  133.                 ' Fourth parameter is not used and should be a zero.
  134.                 SystemParametersInfoA( SPI_SETSTICKYKEYS, 8, BankBuf( bank ), 0 )
  135.        
  136.                 'FreeBank bank - Doesn't seem to be an option to free a bank, so I guess we just let the GC clean it up.
  137.        
  138.                 ' Delete the temporary file used to store the stickykeys flags.
  139.                 If FileType( TStickyKeys.filepath ) = FILETYPE_FILE Then DeleteFile TStickyKeys.filepath
  140.                
  141.         End Function
  142.        
  143. End Type
  144.  
  145.  
  146. ' === DEMO CODE ===
  147.  
  148.  
  149. TStickyKeys.Disable ' Disable the Sticky Keys prompt and functionality.
  150.  
  151. Graphics 800, 600
  152.  
  153. DrawText "Tap your shift key five or more times to test if the Sticky Keys prompt appears.", 10, 10
  154.  
  155. DrawText "If the Sticky Keys prompt doesn't appear then the code is working OK.", 10, 30
  156.  
  157. DrawText "If the Sticky Keys prompt appears then something's gone wrong with the code.", 10, 50
  158.  
  159. DrawText "Press the Escape key or click on the close button to close this window.", 10, 70
  160.  
  161. Flip
  162.  
  163. While ( Not KeyHit ( KEY_ESCAPE ) ) And ( Not AppTerminate() )
  164.         Delay 100
  165. Wend
  166.  
  167. End ' The OnEnd handler set in 'TStickyKeys.Disable' will now cause the user's settings to be restored.


Comments :


Streaksy(Posted 5 months ago)

 YAY


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal