Making Use of Shortcut Keys

Making Use of Shortcut Keys

“Send a Message” (Windows Programming, by Jonathan Zuck,VBPJ December 1995), a great example of code usable ina text editor, mentions three ways to code the Clipboard functionsusing SendMessage, VB’s Clipboard object, or SendKeys. The Clipboardobject requires hand-coding with the SelText property. SendMessageand the Clipboard object require you to specify which text boxis being edited, demanding the ActiveControl property if you havemore than one text box on the form. Clearly the easiest methodis 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 keysto these functions, so the control codes are shown to the rightfor users as in most Windows applications’ Edit menus:

 Undo            Ctrl+Z**************Delete           DelCut                 Ctrl+XCopy               Ctrl+CPaste             Ctrl+V

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

All this seems unnecessary. Windows has the functions built inand I want only to show the shortcuts on the menu so users knowwhat they are. An easy solution is to launch the Notepad, typea Tab, then copy the Tab to the clipboard. Next, open the MenuDesign Window and paste the Tab character followed by the shortcuttext in the menu’s Caption property:

 Caption: &Copy[paste tabchar]Ctrl+C


See also  How College Students Can Shape the Future of Tech Responsibility

 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 thatmenu item and return to it. VB then chops off the Caption at theTab character and you lose your shortcut text. You must repastethe Tab and retype the control code. Or, you could use a menuarray 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 affectedmenu items.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist