Yesterday I discovered the instructable called Making a powerful programmable keypad for less than $30.

And as soon as I read it, I wanted to make one for myself and ordered the required parts. But while waiting for them to arrive, I got thinking about the project. The keypad needs to have its keys remapped in order to output the correct key combinations. This is very fine but I realized that the project requires a small program to stay running in the background for it to work. This is fine for most people but since I use a very old computer, keeping stuff in memory for nothing is not something I like to do. I thought it would be better to have the program open only when I use Photoshop. This got me thinking and I decided to do something about it.

So I created this small batch file. The one I included here is the one I use for my computer so a few changes will be necessary to get it to run properly on your computer. But this is nothing very complicated and should be within reach of anyone who can type.

In short, the script I made starts by checking if the program I want to use with the keypad (Photoshop in my case) and the program required to remap the keys (HID Macros in this case) are running.

If they are not running, it starts them both. If only one is running, it starts the missing piece of the puzzle only. And while they are both running, the script monitors Photoshop to see if it is still running. When you close it, the script sends the proper commands to close HID Macros.

That simple.

Now lets look at the parts.

Step 1: Check to See That the Apps Are Not Running

First, we set-up everything so that it looks nice.

The first echo prevents commands from appearing in the console screen. CLS is just clear screen to make the console clean.

Then, we verify the Windows tasklist to see if our programs are running. The way the scripts work is a bit counter-intuitive since it does not record the presence of the files but the presence of an error due to the programs being there. So, if the apps are running you get an error value of 0 and if they are not, you get an error of 1.

To get the proper name of the running apps, you need to click the Start button and select cmd.

This will get you a console window. Next, start the program you want to know the name of.

All that's left to do is go back to the command window and type tasklist

This will give you the running names of all the programs. It's worth noting that these sometimes differ from their filenames, this is why this step is important.

Step 2: Running Application Checks

One of the biggest disadvantages of using a batch file for doing such a task is how limited it is as a programming language. But this limitation forces the programmer to keep things very simple.

This block contains a series of if-then-else commands. Now due to the simplicity of the language, many of the terms are implicitly used.

The model is the same for every command. If such status is true AND if such other status is true (or not depending on what we want to check), go to a defined part of the program.

So, this section relies of the error statuses that were collected in the previous parts.

The Go To command is used to redirect the script to the proper places. Since the script is designed to run everything from the top down, it's important to prevent the script from doing everything in one long sequence. This is why the sequence sends the program to a label called Stop which is located at the end of the script.

Please note that in the If sequence uses double = signs in order to work. Don't ask me why it is like that, all I know is that it's OK.

Step 3: Running the Programs

Now comes the time to start it all.

The blocks are separated by :Name which indicates the start point.

Followed are a series of start commands. The start command lets Windows know that you want to start a program.

If the program is installed, you can just use its name and you're off the blocks. If not, you need to get the program's full path in order for this to work. One trick is to leave the console window open (cmd) and simply drag and drop your file to the window. This will automatically paste in the right value. Now while in the console window, right click the box and select mark from the list. Now select the proper path with the left button and right click again to copy it to the clipboard. All that's left to do is paste it at the right place.

Now back to the Start command.

This command has many possible switches.

First, there are two "". These can contain the app's title. I left those empty as this feature is not useful in this case.

Then the HID Macros comes the /D switch that sets the working directory. This is followed by the executable name.

I could have omitted the /D switch and put both the path and the executable's name in the same bracket. This would have allowed me to add a link to force the program to open a different file than the base one.

When it comes to Photoshop, you will notice that I used the /wait switch. This tells the script to wait for Photoshop to close before continuing. This feature is handy here since it monitors the programs and allows me to force close everything once you close down Photoshop.

I must point out that I use the echo command to print information on the console window and the pause command to wait until you press any key to close HID Macros.

Step 4: Time to Close It All

Now comes the time to close everything.

To do this, I use the taskkill command. This is a very powerful command and must be used with care.

First, I use the /F switch. This is to force close the program. It won't ask for any prompt or anything, that way no user input is required.

The /IM switch is used to specify the name of the program to close. The name is the same as the one used in step 1. This makes the program work everytime since it doesn't require the user to fetch the process ID in order to close the program.

The taskkill command supports many other switches and even wildcards and is definitely worth researching before customising.

Step 5: Last Word

This script is just the beginning. It can be made as complex or as simple as you want. I hope it will give you inspiration to go and research a bit deeper the lost art of batch scripting.

Now, this script can be beautified using the echo command. It can even be converted into an executable using Advanced BAT to EXE converter which can even put a different icon on the program and make it run invisibly (just make sure you remove any user input like pause from the script).

The sky's the limit!

Thanks to johnofe for this great project. I hope he won't mind me using the photo of his keypad ;-)


EDIT: I have just received my keyboard and proceeded to label all the buttons correctly but upon trying the program out, I noticed that HID Macros didn't capture properly the keys from my keypad. After a lot of searching I discovered that HIDMacros needs to run as administrator (elevated mode) in versions of Windows above XP. I then found a small program called "elevate" that can be put in the same folder as HIDMacros and used to call the macro program. Here is the link Winability's Elevate.

<p>Your batch worked flawlessly, in my case for SketchUp. Thanks to you and to johnofe!</p>
<p>Smart. I like it.</p>
<p>I can't wait to see your version when you have it up and running! </p>
<p>I can't wait either... but it takes a good 3 to 5 weeks for packages from China to reach my doorstep... a long time.</p>

About This Instructable




More by Build-o-Matic:Sony Cyber-Shot battery door improvement Enhancement for the 30$ photoshop keyboard Filters for lenses without threads 
Add instructable to: