Jump to content
Sign in to follow this  
sanyang

Continuous get capslock status

Recommended Posts

sanyang

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

Share this post


Link to post
Share on other sites
Yashied

$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

Share this post


Link to post
Share on other sites
sanyang

Sorry for troubling everyone. I find the way. Just return the variable. :|

Thanks Yashied anyways.

Edited by sanyang

Share this post


Link to post
Share on other sites
nolongthin

@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

Share this post


Link to post
Share on other sites
martin

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.

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.