Thursday, November 12, 2009

Binding Keys to Buttons

One project I worked on required a keyboard handler. Basically a handheld unit was being simulated on the computer and we wanted keyboard shortcuts for all the keys that could be pressed. The only problem with LabVIEW is that buttons on the front panel can only have function keys bound to toggle them.

The solution involved using an event structure and capturing "Key Down" and "Key Up" events, then executing the required functionality. This blog will only be looking at how to handle the key presses. If you only want the button to respond to Key Down events your job is already done you just need to program the required functionality. The "Key Down" event will tell you the following information:

  • Char - the hex value for the ascii character
  • Vkey - ascii or special function such as enter or F7
  • ScanCode - a unique number defining each key on the keyboard

Even having mappings for * and # are fairly simple since the hex values differ to the values for 8 and 3, however they share the same physical key on the keyboard so the ScanCode is the same.

Of course the device keypad had different functionality for pressing and releasing a key. The "Key Up" event does not provide you with the same information as the "Key Down" event, unfortunately the only similar piece of information provided by the "Key Up" event is the ScanCode.

The device also has the ability to hold down multiple keys at once. In order to provide this functionality I buffered all "Key Down" event information using the set variant function. Char and Vkey were combined in a cluster as variant data named with the ScanCode typecast as a string. Then when the "Key Up" event provides a ScanCode it is typecast as a string and pumped into the get variant structure to find the Char released and perform the key release function required (This is important because * and 8 have the same ScanCode so I need to know which one to release based on the Char value generated by "Key Down"). You could use any type of buffer you like, to store the "Key Down" data, I just chose variants because retrieving variant information is order log(n) rather than n for a linear search (Darren's Weekly Nugget 10/09/2006), though probably not significant when considering we have a 10 key/finger limitation.

Monday, November 2, 2009

Custom Controls

Custom buttons in a user interface can make the interface more appealing and easier to navigate. They generally don't require much effort as long as you have reasonable image editing software.

First pick the image you wish to use for the button (This one is courtesy of the gnome user interface and comes under the GNU license be careful with licensing) and ensure it is the required size. This will be the off state image.
Make sure the background is set to be transparent, then save the image as a PNG.
Next copy the image and edit the copy, Create a new layer identical to the bottom layer directly over the top. Fill the top layer foreground with an on/off colour and then blend the images together.
This image will be the on state for the button.
In LabVIEW drop a classic square flat button and right click advanced customize.
In the control front panel click on the spanner to change it to tweezers, then right click on the image import from file and choose your off state image.
Right click the control and select copy the to clipboard.
Right click the control select picture item then the third picture, right click and select import from clipboard. The image is now complete in the off state.

Repeat the process for picture item 2 and 4 with the on state image.

Click the tweezers to switch back to control mode and the custom button is finished.

Followers