Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Expertise: Beginner
Aug 6, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Making Use of Shortcut Keys

"Send a Message" (Windows Programming, by Jonathan Zuck, VBPJ December 1995), a great example of code usable in a text editor, mentions three ways to code the Clipboard functions using SendMessage, VB's Clipboard object, or SendKeys. The Clipboard object requires hand-coding with the SelText property. SendMessage and the Clipboard object require you to specify which text box is being edited, demanding the ActiveControl property if you have more than one text box on the form. Clearly the easiest method is to use SendKeys for Undo (^Z), Delete (Del), Cut (^X), Copy (^C), and Paste (^V), the functions Windows implements natively.

In the Edit menu you'll probably want to assign Shortcut keys to these functions, so the control codes are shown to the right for users as in most Windows applications' Edit menus:

Undo            Ctrl+Z
Delete           Del
Cut                 Ctrl+X
Copy               Ctrl+C
Paste             Ctrl+V

The functions no longer work if your Edit menu has Shortcut keys assigned like this and the Edit subs use SendKeys to access the built-in Windows text-box editing functions. The ^X, ^C, ^V, Del, and ^Z keystrokes are captured by the Edit menu shortcuts and so are never processed. You could use SendMessage, mimic the functions in VB codes using the Clipboard object, or leave off the Shortcut keys.

All this seems unnecessary. Windows has the functions built in and I want only to show the shortcuts on the menu so users know what they are. An easy solution is to launch the Notepad, type a Tab, then copy the Tab to the clipboard. Next, open the Menu Design Window and paste the Tab character followed by the shortcut text in the menu's Caption property:

Caption: &Copy[paste tabchar]Ctrl+C


Caption: &Undo[paste tabchar]Ctrl+Z

Set the Shortcut to (none). Now put the simple SendKeys "^C" in Sub mnuEditCopy and it works!

A design-time problem with this method occurs if you leave that menu item and return to it. VB then chops off the Caption at the Tab character and you lose your shortcut text. You must repaste the Tab and retype the control code. Or, you could use a menu array and Load menu items at run time with embedded Tab characters:

Load mnuEditArray(1)
mnuEditArray(1).Caption = "&Undo" _
        & Chr$(9) & "Ctrl+Z"

I dislike doing anything in code that can be done at design time, so I prefer the former method and avoid clicking on the affected menu items.

Jon Jensen
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date