Jump to content

Hotsetkey With Winactive


Recommended Posts

This script is a example on how to program hotsetkeys so that they will turn on and off depending on if a window is active. It is designed for a beginner to understand how the script works, so please, no comments on the amount of comments in the script. If you see a way to improve the script, but keep the same feel of allowing a beginner to learn, please feel free to comment. Thank you.

last update: 3/23/06

#cs 
Written by Volly - 2006
vollyman@gmail.com

Script for turning on and off HotSetKeys when windows are active or not. The example
below is for controling 3 windows. You can add more windows if needed. 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+  This script is designed so that a beginner will understand what each  +
+ line does. It is designed to teach, so that each line will have a comment +
+ explaining what to do. Feel free to remove any comments you like.      +
+                                                                          +
+                                                                          +
+ Out of kindness, please do not post comments on this script if you feel   +
+ the comments are excessive. It is designed to teach, not to confuse.    +
+ Thank you.                                                                +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#ce========================================================================================
Opt("WinTitleMatchMode", 2);Any part of the string can be used in a window's name
AutoItSetOption("TrayIconDebug", 1);Debug is on. This can be disabled by changing 1 to 0
                            ;If your script hangs, this will help you on what
                            ;is causing the problem.

$window1T = "window 1 title" ;Change "window 1 title" to the title of the first window 
                        ;you wish to use
$window1Tx = "window 1 text" ;Change "window 1 text" to the text of the first window 
                        ;you wish to use
$window2T = "window 2 title" ;Change "window 2 title" to the title of the secondt window 
                        ;you wish to use
$window2Tx = "window 2 text" ;Change "window 2 text" to the text of the second window 
                        ;you wish to use
$window3T = "window 3 title" ;Change "window 3 title" to the title of the third window 
                        ;you wish to use
$window3Tx = "window 3 text" ;Change "window 3 text" to the text of the third window 
                        ;you wish to use    
;if you want to add more windows to control, you can add them here using the naming 
;convention I laid out.

While 1
    Select
        Case WinActive($window1T, $window1Tx)
            _win1_func()
            ContinueLoop
        Case WinActive($window2T, $window2Tx)
            _win2_func()
            ContinueLoop        
        Case WinActive($window3T, $window3Tx)
            _win3_func()
            ContinueLoop
    EndSelect
WEnd

Func _win1_func()
    HotKeySet("{F1}", "_F1A_Func");turns key on for MESSAGE 1A
    HotKeySet("{F5}", "_F5A_Func");turns key on for MESSAGE 2
    HotKeySet("{PRINTSCREEN}", "_key2A_Func");turns key on for MESSAGE 3
    Do
        Sleep(10)
    Until Not WinActive($window1T, $window1Tx)
    _key_clean_1()
    
EndFunc ;==>_win1_func

Func _win2_func()
    HotKeySet("{F1}", "_F1B_Func");turns key on for MESSAGE 1B
    HotKeySet("{F5}", "_F5B_Func");turns key on for MESSAGE 2B
    HotKeySet("{PRINTSCREEN}", "_key2B_Func");turns key on for MESSAGE 3B
    Do
        Sleep(10)
    Until Not WinActive($window2T, $window2Tx)
    _key_clean_1();turns off hotsetkeys
EndFunc ;==>_win2_func

Func _win3_func()
    HotKeySet("{F1}", "_F1C_Func");turns key on for MESSAGE 1C
    HotKeySet("{F5}", "_F5C_Func");turns key on for MESSAGE 2C
    HotKeySet("{PRINTSCREEN}", "_key2C_Func");turns key on for MESSAGE 3C
    Do
        Sleep(10)
    Until Not WinActive($window3T, $window3Tx)
    _key_clean_1()
EndFunc ;==>_win3_func

Func _key_clean_1();Releases hotsetkeys
    HotKeySet("{F1}")
    HotKeySet("{F5}")
    HotKeySet("{PRINTSCREEN}")
EndFunc ;==>_key_clean_1

;the following functions are what happens when a key is pressed

Func _F1A_Func()
MsgBox(0, "", "F1 pressed for Window 1")
EndFunc;==>_F1A_Func

Func _F1B_Func()
MsgBox(0, "", "F1 pressed for Window 2")
EndFunc;==>_F1B_Func

Func _F1C_Func()
MsgBox(0, "", "F1 pressed for Window 3")
EndFunc;==>_F1C_Func

Func _F5A_Func()
MsgBox(0, "", "F5 pressed for Window 1")
EndFunc;==>_F5A_Func

Func _F5B_Func()
MsgBox(0, "", "F5 pressed for Window 2")
EndFunc;==>_F5B_Func

Func _F5C_Func()
MsgBox(0, "", "F5 pressed for Window 3")
EndFunc;==>_F5C_Func

Func _key2A_Func()
MsgBox(0, "", "Print Screen key pressed for Window 1")
EndFunc;==>_key2A_Func

Func _key2B_Func()
MsgBox(0, "", "Print Screen key pressed for Window 2")
EndFunc;==>_key2B_Func

Func _key2C_Func()
MsgBox(0, "", "Print Screen key pressed for Window 3")
EndFunc;==>_key2C_Func
Edited by vollyman
Link to post
Share on other sites

I have a question.. I tried your script and got: ERROR: syntax error

$window1Tx = "window 1 text", ect..

$window1T = "window 1 title", ;Change "window 1 title" to the title of the first window
                           ;you wish to use
$window1Tx = "window 1 text", ;Change "window 1 text" to the text of the first window
                           ;you wish to use
$window2T = "window 2 title", ;Change "window 2 title" to the title of the secondt window
                           ;you wish to use
$window2Tx = "window 2 text", ;Change "window 2 text" to the text of the second window
                           ;you wish to use
$window3T = "window 3 title", ;Change "window 3 title" to the title of the third window
                           ;you wish to use
$window3Tx = "window 3 text", ;Change "window 3 text" to the text of the third window
                           ;you wish to use 
;if you want to add more windows to control, you can add them here using the naming
;convention I laid out.

I'm a little lost on where the windows are.. I tried changing them to notepad, mspaint and wordpad, and still got the error, do I manually add them?

Link to post
Share on other sites

sorry about that. I was editing it, and didn't test it. I fixed it so it should work now. I just tested it by changing the following code, and opening notepad with the word "test" in the edit field

$window1T = "Untitled - Notepad"  ;Change "window 1 title" to the title of the first window 
                            ;you wish to use
$window1Tx = "test"  ;Change "window 1 text" to the text of the first window 
                            ;you wish to use
$window2T = "window 2 title"  ;Change "window 2 title" to the title of the secondt window 
                            ;you wish to use
$window2Tx = "window 2 text"  ;Change "window 2 text" to the text of the second window 
                            ;you wish to use
$window3T = "window 3 title"  ;Change "window 3 title" to the title of the third window 
                            ;you wish to use
$window3Tx = "window 3 text"  ;Change "window 3 text" to the text of the third window 
                            ;you wish to use    
;if you want to add more windows to control, you can add them here using the naming 
;convention I laid out.

While 1
    Select
        Case WinActive($window1T, $window1Tx)
            _win1_func()
            ContinueLoop
;       Case WinActive($window2T, $window2Tx)
;           _win2_func()
;           ContinueLoop        
;       Case WinActive($window3T, $window3Tx)
;           _win3_func()
;           ContinueLoop
    EndSelect
WEnd
Edited by vollyman
Link to post
Share on other sites
  • 2 months later...

That kind of eats at your CPU. All that window checking and hotkey setting? Why not use what I posted back in december?

HotKeySet("{F2}", "HotKeyFunc")
HotKeySet("{ESC}", "HotKeyFunc")

While 1
    Sleep(60000)
    ; Script part
WEnd

Func HotKeyFunc()
    If WinActive("") Then; Set the window
        Switch @HotKeyPressed
            Case "{F2}"
                ;;;
            Case "{ESC}"
                Exit
        EndSwitch
    Else
        HotKeySet(@HotKeyPressed)
        Send(@HotKeyPressed)
        HotKeySet(@HotKeyPressed, "HotKeyFunc")
    EndIf
EndFunc

Link to post
Share on other sites
  • 3 weeks later...

@gamerman2360

Your function is exactly what I needed. It is quite useful. I had remembered seeing it, and found this post with it.

I just wanted to let you know I appreciate it!

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...