APPENDIX: ACCELERATOR Keys
Updated: Sep 20, 2007

Q: What about Accelerators?

A: Right, take Accelerators... Please ... take them.

HB supports accelerator keys; please see
HotIDE menus; and they can be for menu items or "hot keys" to do
any other action in the application, for that matter.  I've
posted snippets of HotIDE code a la "accelerators made easy" and
could repost or work up a simple example.

Windows' Accelerator Tables might be doable also, but clearly
these are an artifact of a by-gone era -- when nobody including
Microsoft, had a "GUI-aware" compiler, in any language, the
pre-HotBasic days.

The "meta-issue" here seems to be that Accelerator Tables and
associated API functions were developed to ease the pain
associated with absence of any GUI-aware compiler in any
language.  [For historians the question remains, why did not
Microsoft or others just write a GUI-aware compiler instead?]
As a result, in this analysis, yet another Windows' OS service
was created to allow programmers to ask the OS to "do it for me"
-- however, this "ask John to do Paul's work" solution is now
all but obsolete with the advent of HotBasic [ref. again to
HotIDE which has its own "accelerator tables" built into code
"the easy way".]

Looking at Accelerator Tables ("the pre-GUI-aware hard way")
this morning I can see what might be expected in this "I can't
do myself; I'll ask the OS" solution for a by-gone era.  You
need to know the ID's of the menu items!  Oops.  Definitely a
pre-GUI-aware game!  HotBasic automatically assigns those ID's
as code is compiled and run!  HotBasic takes care of all of this
drudgery -- this mindless book-keeping, so coders can
concentrate on the "big issues".

Back to the Tables, what a clumsy solution, in retrospect.  I
suppose someone could attempt a "back to the past" effort --
make the resource after a test version of program has printed
out the ID's so you know what they are; at startup use the API
to load this table from the resources; and attempt to call the
TranslateAccelerator thingy in your .onkeyup and .onkeydown
routines.  Definitely passe stuff.  In retrospect, it is amazing
the shear pain that coders had doing GUI app's prior to the
GUI-aware compiler *even with Accelerator Tables*, where
separate pieces (the resource, the load, the message loop
work-around) are obviously less efficient, and more difficult to
maintain as a program develops.

In short, it strikes me that an attempt to use Accelerator
Tables might be an interesting "walk down memory lane", but the
HotBasic "easy way" is probably preferable given today's
pressures to get the job done with efficiency (including the
project development phase).

Glad you brought up the subject.  I refer to HotIDE again as an
example: "Look ma, no messy Accelerator Table; I can do all ...
and look, ... no hands!"

In .Onkeyup and .Onkeydown procedures, keyboard accelerator code
generally maintains internal variables like iCTRL to flag
whether a particular "special key", such as CTRL, SHIFT, etc, is
pressed.   Then if pressed, a SELECT CASE simply uses GOTO to
the desired procedure (the "command") for each key (s for
ctrl-s, c for ctrl-c, etc).  If not, the key routines simply
proceed on for any further desired processing of the key.
The code is simple to write and maintain and develop (like add a
new "accelerator key" is just another entry in the SELECT CASE
block).

No resource file, no worry about ID's, nothing to read and learn
(like a chapter on Accelerator file format and usage), no
special startup code to load a resource, no funny-stuff (really
a Windows' work-around to do it the "hard way") to Translate
anything screwing up the message loop; no additional dependence
on the OS.  Whew!

In summary, Accelerator Tables may well be an excellent example
of what we archetypically hear from our parents, "If you only
knew how hard it was in the old days and how easy you have it
now."

Code example:

defint vKey,iCTRL,iSHIFT  'and whatever you need
'.OnKeyUp and .OnKeyDown routines:

key_up:
vKey=wParam
'do accelerators first
IF vKey=VK_CTRL THEN iCTRL=0: return
IF vKey=VK_SHIFT THEN iSHIFT=0: return
'anything else
return 

key_down:
vKey=wParam
'do accelerators first
IF vKey=VK_CTRL THEN iCTRL=1: return
IF vKey=VK_SHIFT THEN iSHIFT=1: return
IF iCNTL THEN
  SELECT CASE vKey
  CASE VK_S: Go to Process_Ctrl_S  'Look Ma, no ID's
  'etc
  END SELECT
END IF
'same for others like iSHIFT
'IF iCTRL and iSHIFT THEN  '''for Ctrl-Shift example
'select case here
'END IF  'for Ctrl-Shift
'process non-accelerator keys here
return 

hotkey.inc with the hotkey.bas demo showing usage (in HotInclude
download) is a similar approach to easy processing of accelerator
keys.


Copyright 2006-2007 James J Keene PhD
Original Publication: Mar 12, 2006
