Sign in to follow this  
Followers 0
amcdee

Window focus change

6 posts in this topic

Hi guys,

Sorry a newbie here so please be gentle with me. I'm trying to write a script that will change the state of the capslock based on the active window.

I've got this so far..

CODE
;~ Capslock Changer

while 1

Opt("WinTitleMatchMode", 1)

if WinwaitActive("Untitled ","") and _GetCapslock() = 0 then CapsOn()

Opt("SendCapslockMode",0)

Send("{CAPSLOCK off}"); 0 = Off / 1 = On

;MsgBox(1,"","sage is active")

WinWaitNotActive("Untitled ","")

wend

Func _GetCapslock(); Checks to see if caps Lock is on

Local $ret

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

Return $ret[0]

EndFunc

Func CapsOn()

Opt("SendCapslockMode",1)

Send("{CAPSLOCK on}"); 0 = Off / 1 = On

;MsgBox(1,"","sage is active")

EndFunc

The problem is... onec the window gains focus.. the the capslock flash repeatly...

ideal I need to detect a change of window focus and then set the capslock appropriately.. but I can't work this out...

Any help would be very much appropriated.

Kindest regards,

Amcdee

Share this post


Link to post
Share on other sites



Hi guys,

Sorry a newbie here so please be gentle with me. I'm trying to write a script that will change the state of the capslock based on the active window.

I've got this so far..

CODE
;~ Capslock Changer

while 1

Opt("WinTitleMatchMode", 1)

if WinwaitActive("Untitled ","") and _GetCapslock() = 0 then CapsOn()

Opt("SendCapslockMode",0)

Send("{CAPSLOCK off}"); 0 = Off / 1 = On

;MsgBox(1,"","sage is active")

WinWaitNotActive("Untitled ","")

wend

Func _GetCapslock(); Checks to see if caps Lock is on

Local $ret

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

Return $ret[0]

EndFunc

Func CapsOn()

Opt("SendCapslockMode",1)

Send("{CAPSLOCK on}"); 0 = Off / 1 = On

;MsgBox(1,"","sage is active")

EndFunc

The problem is... onec the window gains focus.. the the capslock flash repeatly...

ideal I need to detect a change of window focus and then set the capslock appropriately.. but I can't work this out...

Any help would be very much appropriated.

Kindest regards,

Amcdee

Hi! Try this:

Global $VK_CAPITAL = 0x14

Run("notepad.exe")

WinWaitActive("[Class:Notepad]")

While 1
    If (WinActive("[Class:Notepad]") = 1) And (_GetCapslock() = 0) Then
        CapsOn()
    ElseIf (WinActive("[Class:Notepad]") = 0) And (_GetCapslock() = 1) Then
        Send("{CAPSLOCK off}")
    EndIf
    Sleep(50)
WEnd

Func _GetCapslock()
    Local $aRet = DllCall("user32.dll", "int", "GetKeyState", "int", $VK_CAPITAL)
    Return $aRet[0]
EndFunc

Func CapsOn()
    Opt("SendCapslockMode", 0)
    Send("{CAPSLOCK on}")
EndFunc

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi! Try this:

Global $VK_CAPITAL = 0x14

Run("notepad.exe")

WinWaitActive("[Class:Notepad]")

While 1
    If (WinActive("[Class:Notepad]") = 1) And (_GetCapslock() = 0) Then
        CapsOn()
    ElseIf (WinActive("[Class:Notepad]") = 0) And (_GetCapslock() = 1) Then
        Send("{CAPSLOCK off}")
    EndIf
    Sleep(50)
WEnd

Func _GetCapslock()
    Local $aRet = DllCall("user32.dll", "int", "GetKeyState", "int", $VK_CAPITAL)
    Return $aRet[0]
EndFunc

Func CapsOn()
    Opt("SendCapslockMode", 0)
    Send("{CAPSLOCK on}")
EndFunc

Thanks mate, works a treat, but you can't switch capslock on if any other window is active? any clues...

thanks again

Amcdee

Edited by amcdee

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thanks mate, works a treat, but you can't switch capslock on if any other window is active? any clues...

thanks again

Amcdee

Looks to me like all you need is

Global $VK_CAPITAL = 0x14

Run("notepad.exe")
Opt("SendCapslockMode", 0)

Global $PreviousCAPSOFF;CAPSLOCK OFF
While 1
    WinWaitActive("[Class:Notepad]")
    $PreviousCAPSOFF = _GetCapslock() = 0
    If  $PreviousCAPSOFF Then  Send("{CAPSLOCK on}")
    
    While WinActive("[Class:Notepad]")
        Sleep(100)
    WEnd
    If $PreviousCAPSOFF Then Send("{CAPSLOCK off}")
WEnd

Func _GetCapslock()
    Local $aRet = DllCall("user32.dll", "int", "GetKeyState", "int", $VK_CAPITAL)
    Return $aRet[0]
EndFunc
Edited by martin

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

Or:

Opt("SendCapslockMode", 0)

Global Const $VK_CAPITAL = 0x14

Global $On = False

Run("notepad.exe")
WinWaitActive("[Class:Notepad]")

While 1
    If (WinActive("[Class:Notepad]") = 1) And (_GetCapslock() = 0) Then
        CapsOn()
    ElseIf (WinActive("[Class:Notepad]") = 0) And (_GetCapslock() = 1) Then
        If $On = True Then ContinueLoop
        $On = True
        Send("{CAPSLOCK off}")
    EndIf
    Sleep(50)
WEnd

Func _GetCapslock()
    Local $aRet = DllCall("user32.dll", "int", "GetKeyState", "int", $VK_CAPITAL)
    Return $aRet[0]
EndFunc

Func CapsOn()
    $On = False
    Send("{CAPSLOCK on}")
EndFunc
:)

Share this post


Link to post
Share on other sites

Or:

Opt("SendCapslockMode", 0)

Global Const $VK_CAPITAL = 0x14

Global $On = False

Run("notepad.exe")
WinWaitActive("[Class:Notepad]")

While 1
    If (WinActive("[Class:Notepad]") = 1) And (_GetCapslock() = 0) Then
        CapsOn()
    ElseIf (WinActive("[Class:Notepad]") = 0) And (_GetCapslock() = 1) Then
        If $On = True Then ContinueLoop
        $On = True
        Send("{CAPSLOCK off}")
    EndIf
    Sleep(50)
WEnd

Func _GetCapslock()
    Local $aRet = DllCall("user32.dll", "int", "GetKeyState", "int", $VK_CAPITAL)
    Return $aRet[0]
EndFunc

Func CapsOn()
    $On = False
    Send("{CAPSLOCK on}")
EndFunc
:)
Thanks Guys worked a treat.. :(

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  
Followers 0