Jump to content

Continuous get capslock status


Recommended Posts

I'm currently use this to get status of capslock

$Cslstt = DllCall("user32.dll","long","GetKeyState","long",0x14)

I want to make this checks continuously but when I put it in the loop, the $Cslstt[0] only equals to the first state of Capslock when I run the program (0 or 1). It means even I toggle capslock, the variable is still the same. I'm a noob here, so is there anyone kind enough to explain the reason to me, and tell me how I can correct this?

PS: When I put it in a loop, the capslock LED toggles like crazy.

Edited by sanyang
Link to comment
Share on other sites

$pState = _GetCapsLockState()

While 1
    $nState = _GetCapsLockState()
    If $nState <> $pState Then
        ConsoleWrite('CAPSLOCK: ' & $nState & @CR)
        $pState = $nState
    EndIf
    Sleep(100)
WEnd

Func _GetCapsLockState()

    Local $Ret = DllCall('user32.dll', 'int', 'GetKeyState', 'int', 0x14)

    If @error Then
        Return SetError(1, 0, 0)
    EndIf
    Return BitAND($Ret[0], 1)
EndFunc   ;==>_GetCapsLockState

Link to comment
Share on other sites

  • 2 years later...

@Yashied, Thanks for your code but am trying to do this if its on it writes capslock on, and if off it writes capslock off :

$pState = _GetCapsLockState()
$caps = caps()
Func caps()
   if  $pState = 1 then 
      ConsoleWrite("{CAPSLOCK ON}") ;CAPSLOCK
   Else
      ConsoleWrite("{CAPSLOCK OFF}") ;CAPSLOCK
   EndIf
EndFunc  



 
While 1
    $nState = _GetCapsLockState()
    $caps = caps()
    ;If $nState <> $pState Then
     ;   ConsoleWrite('CAPSLOCK: ' & $nState & @CR)
       $pState = $nState
  ; EndIf
    Sleep(100)
WEnd

Func _GetCapsLockState()

    Local $Ret = DllCall('user32.dll', 'int', 'GetKeyState', 'int', 0x14)

    If @error Then
        Return SetError(1, 0, 0)
    EndIf
    Return BitAND($Ret[0], 1)
EndFunc   ;==>_GetCapsLockState

I keep getting this

>"H:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "H:\personal key\v4\test\tes.au3"
{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}{CAPSLOCK OFF}
>Process failed to respond; forcing abrupt termination...
>Exit code: 1    Time: 4.339

Please help i want it to show like this:

>"H:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "H:\personal key\v4\test\tes.au3"
{CAPSLOCK OFF}
{CAPSLOCK ON}
>Process failed to respond; forcing abrupt termination...
>Exit code: 1    Time: 4.339

Thanks

Link to comment
Share on other sites

I think your code works, but it would be easier to see if you only printed when the casps lock state changed. Something like this

$lastState = -1

$pState = _GetCapsLockState()
caps()


While 1
    $pState = _GetCapsLockState()
    caps()
    Sleep(100)
WEnd

Func _GetCapsLockState()

    Local $Ret = DllCall('user32.dll', 'int', 'GetKeyState', 'int', 0x14)

    If @error Then
        Return SetError(1, 0, 0)
    EndIf
    Return BitAND($Ret[0], 1)
EndFunc   ;==>_GetCapsLockState

Func caps()
    If $pState = $lastState Then Return
    If $pState = 1 Then
        ConsoleWrite("{CAPSLOCK ON}" & @CR) ;CAPSLOCK
    Else
        ConsoleWrite("{CAPSLOCK OFF}" & @CR) ;CAPSLOCK
    EndIf
    $lastState = $pState
EndFunc   ;==>caps
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...