Beginner Lesson 2 - Creating A Shutdown/Log Off/Lock Program
Using Hot keys Effectivley
In this lesson, we will be looking into how we can properly use AutoIt to check for different keys without making a ton o functions for each.
Let's Get Started
Open SciTE and make a new file called something along the lines of "HotKeyTest.au3"
Now type in the following...
What we are doing here, is running a function called hotkeypress, which we have yet to create. Notice we do this for all the different keys. We do this because we will access which keys were pressed in this function we will create in a moment.
Modifier Keys
^ = CTRL
! = ALT
+ = SHIFT
# = WIN
In my case, I am checking for CTRL+ALT+O, CTRL+ALT+L and CTRL+ALT+S. Remember, always to specify a non modifier key as well if you use any of the modifier keys, or else you will find nothing will happen. If you want to use a set system key, you must encase it in braces { }. Say if I want to use the home key, I would do this...
Modifier Keys
^ = CTRL
! = ALT
+ = SHIFT
# = WIN
In my case, I am checking for CTRL+ALT+O, CTRL+ALT+L and CTRL+ALT+S. Remember, always to specify a non modifier key as well if you use any of the modifier keys, or else you will find nothing will happen. If you want to use a set system key, you must encase it in braces { }. Say if I want to use the home key, I would do this...
This will apply to many other keys, such as the function keys; F1, F2, F3, e.t.c.
Let's now create that function. Type the following, indicated by the blue rectangle.
Let's now create that function. Type the following, indicated by the blue rectangle.
We create or 'open' the function by using the 'Func' keyword. We can't use parameters with HotKeySet, so just put empty parentheses, you must put these in regardless.
We create a local variable inside the function and set it to the @HotKeyPressed function or macro; if you will, that AutoIt provides us. We make it Local for one simple reason. We are not interested in using it anywhere else in the script, but this function. It is good practise to use the 'Local' keyword when initialising a variable, so you know that it is the declaration of the variable and not just the setting to a value.
Now, we could use an if statement with several elseif's, but this can get messy and hard to read. AutoIt provides a better solution designed specifically for checking many scenarios like this.
We can use a switch statement by using the 'Switch' keyword and giving it an expression. In this case, we give it the data stored inside the $hk variable we created.
Now it is time to check a few possible cases, see how it is appropriately named?
So, for our first case, let's check if the keys CTRL+ALT+O was pressed, if this was in fact true, then we run another function that we have not yet created and give it the parameter of number 0, you will see why shortly.
We do the same for the other two hot key cases and run the same function with different parameters.
Whenever you open a Switch, you must close it, do this by using EndSwitch. Do this also for the function by using the EndFunc keyword.
We can now create that function we called with different numbers inside.
Type the following shown inside the blue rectangle.
We create a local variable inside the function and set it to the @HotKeyPressed function or macro; if you will, that AutoIt provides us. We make it Local for one simple reason. We are not interested in using it anywhere else in the script, but this function. It is good practise to use the 'Local' keyword when initialising a variable, so you know that it is the declaration of the variable and not just the setting to a value.
Now, we could use an if statement with several elseif's, but this can get messy and hard to read. AutoIt provides a better solution designed specifically for checking many scenarios like this.
We can use a switch statement by using the 'Switch' keyword and giving it an expression. In this case, we give it the data stored inside the $hk variable we created.
Now it is time to check a few possible cases, see how it is appropriately named?
So, for our first case, let's check if the keys CTRL+ALT+O was pressed, if this was in fact true, then we run another function that we have not yet created and give it the parameter of number 0, you will see why shortly.
We do the same for the other two hot key cases and run the same function with different parameters.
Whenever you open a Switch, you must close it, do this by using EndSwitch. Do this also for the function by using the EndFunc keyword.
We can now create that function we called with different numbers inside.
Type the following shown inside the blue rectangle.
As before, we open a function, but this time we give or ask for a parameter. I will call this $code, respectively, but this can be anything, e.g, $cabbageisavegetable, but try to name it something relevant for obvious reasons.
We will show a MsgBox to make sure the user wants to carry on with the operation, or subroutine; if you're that way inclined.
MsgBoxs return a integer value, which translates to be the button constant clicked in the MsgBox. Instead of letting that go to waste, we can use that to determine whether to go on after the MsgBox has shown.
We make the MsgBox call as part of an if statement. We call with the first parameter being 4, which is the decimal for YES and NO buttons. We do this because we want to give the user a choice, if with called with the parameter 1, which means show a MsgBox with the OK button only, we wouldn't be giving an option.
We give the MsgBox a title, text and then concatenate (join a string to another string) a string returned from a function we need to make. This function will convert the numbers into a descriptive synopsis of what the hot key(s) that were pressed will do.
We check if the MsgBox returned the value of 6, which is the 'YES' button. If so, then we carry on.
We will do another check inside the other one (called a nested if) which checks the value of the $code parameter the function was called, or run with. If it was run with the number 2 then we have to do something out of the ordinary to be able to lock the computer, as the shutdown class provided by AutoIt does not contain the lock function. We can use Window's API, another word for function, to lock the computer the way windows itself does.
We run a file called rundll32.exe with a parameter or argument. This parameter/argument will be the name of the library that contains that computer-locking functionality. In this case, it is the user32.dll library. Use a comma and then give the function name you would like to run.
Then we use and else to run the ordinary method of shutting down and logging off which AutoIt provides us when the parameter variable is anything but number 2.
Close this if as before, we must close what we open. Do the same for the function.
Now write the following in the blue rectangle.
We will show a MsgBox to make sure the user wants to carry on with the operation, or subroutine; if you're that way inclined.
MsgBoxs return a integer value, which translates to be the button constant clicked in the MsgBox. Instead of letting that go to waste, we can use that to determine whether to go on after the MsgBox has shown.
We make the MsgBox call as part of an if statement. We call with the first parameter being 4, which is the decimal for YES and NO buttons. We do this because we want to give the user a choice, if with called with the parameter 1, which means show a MsgBox with the OK button only, we wouldn't be giving an option.
We give the MsgBox a title, text and then concatenate (join a string to another string) a string returned from a function we need to make. This function will convert the numbers into a descriptive synopsis of what the hot key(s) that were pressed will do.
We check if the MsgBox returned the value of 6, which is the 'YES' button. If so, then we carry on.
We will do another check inside the other one (called a nested if) which checks the value of the $code parameter the function was called, or run with. If it was run with the number 2 then we have to do something out of the ordinary to be able to lock the computer, as the shutdown class provided by AutoIt does not contain the lock function. We can use Window's API, another word for function, to lock the computer the way windows itself does.
We run a file called rundll32.exe with a parameter or argument. This parameter/argument will be the name of the library that contains that computer-locking functionality. In this case, it is the user32.dll library. Use a comma and then give the function name you would like to run.
Then we use and else to run the ordinary method of shutting down and logging off which AutoIt provides us when the parameter variable is anything but number 2.
Close this if as before, we must close what we open. Do the same for the function.
Now write the following in the blue rectangle.
This is the function we said we will create to return a text equivalent of what the number value will do.
We will use another Switch Statement to test multiple conditions. In this case, we test the parameter the function was called with.
In the case that that it is number 0, we will return, or send back "Log Off". Remember you must use the 'Return' keyword to send something back as a result of running this function.
We do the same in the event that it is called with number 1, and number 2, but send back the string that is appropriate.
Then end the switch and function as always.
We are now basically done, all we need to do is create idle loop, or infinite loop to prevent the script from exiting after having run all the code. This way it will be alive and 'listening' for the hot keys that may be pressed.
Type in the following at the bottom of the script, below everything else.
We will use another Switch Statement to test multiple conditions. In this case, we test the parameter the function was called with.
In the case that that it is number 0, we will return, or send back "Log Off". Remember you must use the 'Return' keyword to send something back as a result of running this function.
We do the same in the event that it is called with number 1, and number 2, but send back the string that is appropriate.
Then end the switch and function as always.
We are now basically done, all we need to do is create idle loop, or infinite loop to prevent the script from exiting after having run all the code. This way it will be alive and 'listening' for the hot keys that may be pressed.
Type in the following at the bottom of the script, below everything else.
We can make infinite loop by using a while loop with the expression 1, which means 'True'. You may also use the keyword 'True' if you want.
Always sleep your loops for a brief moment, even as little as 10 ms, or else your CPU will be strained 100%. Adding a short sleep will make it so your CPU is barely used. I chose to use 250 ms.
Now end you while loop which you opened. Do this always!
Now we can test our program or script.
Go to Tools > Go.
Always sleep your loops for a brief moment, even as little as 10 ms, or else your CPU will be strained 100%. Adding a short sleep will make it so your CPU is barely used. I chose to use 250 ms.
Now end you while loop which you opened. Do this always!
Now we can test our program or script.
Go to Tools > Go.
Now when you press one of your hot keys, the script logic will access which keys were pressed and will do something differently accordingly. Try pressing CTRL+ALT+O.
You can see that we get a MsgBox displays the correct "Log Off" in this case. If we press no, the MsgBox will close and continue to listen for keys again.
Now try pressing CTRL+ALT+L
Now try pressing CTRL+ALT+L
You can see this has the correct text as well, which is "Lock". Press YES this time and see if it works.
If, you are using windows 7, then you should see this. This took some research on how to capture the login screen, so I might make a tutorial on that as it proved more difficult that I first imaged. I used PSTools and Boxcutter then had to create a .bat script to take screenshot after a few seconds to give me time to close the cmd.exe window.
Try the same with the shutdown hot key to check if everything is working and then you are done.
If you would like to download the source code for this file you can do so by hitting the download button below.
Try the same with the shutdown hot key to check if everything is working and then you are done.
If you would like to download the source code for this file you can do so by hitting the download button below.
Author: Riccardo Geraci, 25.09.14