Jump to content
Sign in to follow this  
icedice

Boolean not changing value?

Recommended Posts

icedice

I'm working on a script to write stuff to a file. The problem comes in when I try to write stuff in uppercase or lowercase depending on if the user has caps lock or a shift key pushed. When someone pushed the shit of caps lock key it should make the Boolean $isCaps be true or false depending on what it is before they press the key.

Here's my function:

Func _checkCaps()
   If $isCaps = False Then
   If _IsPressed('14') Then ;Capslock key
   $isCaps = True
   EndIf
  
   If _IsPressed('10') Then ;Shift key
   $isCaps = True
   EndIf
  
   If _IsPressed('A0') Then ;Shift key
   $isCaps = True
   EndIf
  
   If _IsPressed('A1') Then ;Shift key
   $isCaps = True
   EndIf
   Else
   If _IsPressed('14') Then ;Capslock key
   $isCaps = False
   EndIf
  
   If Not _IsPressed('10') Then ;Shift key
   $isCaps = False
   EndIf
   If Not _IsPressed('A0') Then ;Shift key
   $isCaps = False
   EndIf
   If Not _IsPressed('A1') Then ;Shift key
  $isCaps = False
   EndIf
   EndIf
  
   MsgBox(0, "Message Box", "Caps is now " & $isCaps)
EndFunc

When you press shift or caps lock the msgbox pops up and says "Caps is now False" no matter what it was before.

This is only my second day or writing in autoit so it's probably something really easy that I am looking over. All help is appreciated!

Share this post


Link to post
Share on other sites
Shimgreen

Try this, should be easier and better.

Instead of using the user32 to retrieve when the shift or caps is pushed, just have it retrieve the current state...

Global Const $VK_CAPITAL = 0x14

Global Const $VK_LSHIFT = 0xA0

Global Const $VK_RSHIFT = 0xA1

DllCall("user32.dll","long","GetKeyState","long",$VK_CAPITAL)

DllCall("user32.dll","long","GetKeyState","long",$VK_LSHIFT)

DllCall("user32.dll","long","GetKeyState","long",$VK_RSHIFT)

if you need any more help let me know.

Share this post


Link to post
Share on other sites
Spiff59

Not sure if I'm recreating the wheel, but I put this together that seems to work fairly well:

While 1
    $aCapState = _GetCapState()
    Tooltip("CapsLockDown = " & $aCapState[0] & "   CapsLockState = " & $aCapState[1] & "   ShiftDown = " & $aCapState[2] & "   CAPSTATE = " & $aCapState[3])
WEnd

; #FUNCTION# =======================================================================================================================
; Name...........: _GetCapState
; Description ...: Determine current state of CapsLock key, Shift key, CapsLock mode, and if keyboard is in uppercase mode
; Syntax.........: _GetCapState()
; Parameters ....: none
; Return values .: Returns a 1-dimension 0-based array containing four elements
;     Elenent 0: Current status of CapsLock key (1 = down)
;     Elenent 1: Current status of keyboard CapsLock mode (1 = on)
;     Elenent 2: Current status of either Shift key (1 = down)
;     Elenent 3: Overall case status of keyboard (1 = uppercase)
; Author ........: Spiff59
; ==================================================================================================================================
Func _GetCapState()
    If Not IsDeclared("__aGetCapState") Then Global $__aGetCapState[4]
    Local $CapLockKey = DllCall("user32.dll", "long", "GetKeyState", "long", 0x14)
    If Abs($CapLockKey[0]) > 1 Then ; CapsLock key is down
        If Not $__aGetCapState[0] Then ; only toggle state when first pressed
            $__aGetCapState[1] = (Not $__aGetCapState[1]) * 1
            $__aGetCapState[0] = 1
        EndIf
    Else ; CapsLock key is up
        $__aGetCapState[1] = $CapLockKey[0]
        $__aGetCapState[0] = 0
    EndIf
    Local $ShiftKey = DllCall("user32.dll", "long", "GetKeyState", "long", 0x10)
    $__aGetCapState[2] = (Abs($ShiftKey[0]) > 1) * 1 ; if 1, a Shift key is down
    $__aGetCapState[3] = Abs($__aGetCapState[1] - $__aGetCapState[2]) ; combine CapsLock and Shift
    Return $__aGetCapState
EndFunc

Edit: Made it more UDF-like, although it does require 2 globals

Edit2: Got rid of the requirement for predefined external Globals

Edit3: Removed Misc.au3 UDF. It is not required.

Edited by Spiff59

Share this post


Link to post
Share on other sites
icedice

Not sure if I'm recreating the wheel, but I put this together that seems to work fairly well:

#include <Misc.au3>

While 1
    $aCapState = _GetCapState()
    Tooltip("CapsLockDomn = " & $aCapState[0] & "   CapsLockState = " & $aCapState[1] & "   ShiftDown = " & $aCapState[2] & "   CAPSTATE = " & $aCapState[3])
WEnd

; #FUNCTION# =======================================================================================================================
; Name...........: _GatCapState
; Description ...: Determine current state of CapsLock key, Shift key, CapsLock mode, and if keyboard is in uppercase mode
; Syntax.........: _GetCapsState()
; Parameters ....: none
; Return values .: Returns a 1-dimension 0-based array containing four elements
;     Elenent 0: Current status of CapsLock key (1 = down)
;     Elenent 1: Current status of keyboard CapsLock mode (1 = on)
;     Elenent 2: Current status of either Shift key (1 = down)
;     Elenent 3: Overall case status of keyboard (1 = uppercase)
; Required UDF  : Misc.au3
; Author ........: Spiff59
; ==================================================================================================================================
Func _GetCapState()
    If Not IsDeclared("__aGetCapState") Then Global $__aGetCapState[4]
    Local $CapLockKey = DllCall("user32.dll", "long", "GetKeyState", "long", 0x14)
    If Abs($CapLockKey[0]) > 1 Then ; CapsLock key is down
        If Not $__aGetCapState[0] Then ; only toggle state when first pressed
            $__aGetCapState[1] = (Not $__aGetCapState[1]) * 1
            $__aGetCapState[0] = 1
        EndIf
    Else ; CapsLock key is up
        $__aGetCapState[1] = $CapLockKey[0]
        $__aGetCapState[0] = 0
    EndIf
    Local $ShiftKey = DllCall("user32.dll", "long", "GetKeyState", "long", 0x10)
    $__aGetCapState[2] = (Abs($ShiftKey[0]) > 1) * 1 ; a Shift key is down
    $__aGetCapState[3] = Abs($__aGetCapState[1] - $__aGetCapState[2]) ; combine CapsLock and Shift
    Return $__aGetCapState
EndFunc

Edit: Made it more UDF-like, although it does require 2 globals :oops:

Edit2: Got rid of the requirement for predefined external Globals

Thank you it works like a charm!

Share this post


Link to post
Share on other sites
nolongthin

@Spiff59 I want to ask is there a way to make it that if i type ABCD it would know my caps lock is on and console write it this way in all UPERCASE and if i write abcd it would console write it in LOWERCASE.

Please help

Share this post


Link to post
Share on other sites
Spiff59

@Spiff59 I want to ask is there a way to make it that if i type ABCD it would know my caps lock is on and console write it this way in all UPERCASE and if i write abcd it would console write it in LOWERCASE.

Please help

I'm a little confused by your question.  The function above monitors the real-time state of a few keys and determines the overall CAPS state of the keyboard.  Basic input functions should return text reflective of the keyboard's state when the text was entered, and, you're entirely in control of what you might send to the ConsoleWrite() function.

If you're using lower-level input routines like _IsPressed() or trapping keydown events, then posting some example code would be useful.

Edited by Spiff59

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  

×