The new vbAccelerator Site - more VB and .NET Code and Controls
Source Code
3 Code Libraries &nbsp
&nbsp

vbAccelerator Accelerator Control

 
 

Intercept all the accelerator keys on the keyboard, and set accelerators up at design time.

 


 NOTE: this code has been superceded by the version at the new site.



&nbsp

Alt-Left Arrow Accelerator in VB

Download the vbAccelerator Accelerator Control, Source and Demo (34kb)


Overview
If you've ever tried to pick a keyboard accelerator for the menu items Back and Next in your application, you might have discovered a limitation in VB's ability to set up accelerators. IE offers some sensibly chosen keyboard accelerators for its Back, Next, Home shortcuts, as shown in the image above, but you won't be able to set them up in standard VB!

Another thing that can be annoying is that you cannot set up accelerators at run-time, only at design time. This means if you are loading custom menus then you won't be able to give them an accelerator. Well, you could try using a Registered HotKey, but remember that HotKeys are system-wide in scope, so you might end up triggering the key when you don't want to! And what if you have two applications with the same accelerator?


The vbAccelerator Accelerator Control
This control uses a Windows Hook to intercept keyboard messages whilst your application is running. Because it affects all windows within your application's thread, it doesn't matter which window has focus, the accelerator will also be detected. (So beware! Don't, for example, set up an accelerator on the 'A' key...)

Using the control is simple. Just drop one control on your application's main form and then call the AddAccelerator method with the key code, shift constants added together and a string property to identify the accelerator. Then whenever the accelerator is pressed the control's Accelerator event will fire, passing you the index of the accelerator in the control's array. You can get the string key property which identifies the accelerator from the index with the Key property.

To indicate that an accelerator is associated with a menu, you can set the caption of the menu to show the accelerator. To do this, just set the caption property of the menu to the regular caption plus a tab plus the text for the accelerator. So for example to set up a Home menu item with Alt+Home as the displayed accelerator, do this:

   mnu.Caption = "&Home Page" & vbTab & "Alt+Home"


Why Is This an OCX and Not a DLL?
Normally I would code this sort of component as a DLL because you don't need the overhead of a control implementation to achieve this (particularly when there are no property pages or real design-time features).

The main problem with implementing accelerators is that they can interfere with the IDE. What happens if you set up an accelerator on the Ctrl+Break key combination? With a Windows Hook, this can eat the message before it gets to the VB IDE, leaving you with no way to interrupt the code whilst it is running.

There appears to be only one solution to this in VB: to check the EventsFrozen property of a the Ambient object. But the Ambient object is only available in a UserControl. Having said that, the Debug Object for AddressOf Subclassing available from the MSDN Visual Basic owner's area seems to solve this problem. Anyone got an idea how its done?



TopBack to top
Source Code - What We're About!Back to Source Code


&nbsp
 

About  Contribute  Send Feedback  Privacy

Copyright © 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 25 August 1999