User Tools

Site Tools


creating_20function_20key_20macros

Creating function key macros

by Richard Russell, December 2008

In the BBC BASIC for Windows Integrated Development Environment (i.e program editor and user interface) function keys F9 to F12 have no effect, by default. The same is true of function keys F1 to F8 when used in conjunction with the Shift key. However it is possible to customise these keys so that when pressed they execute a keyboard macro, that is have the same effect as pressing a sequence of keys on the keyboard.

The most straightforward way of creating a keyboard macro is probably to use the Macro Recorder utility which can be found here. An alternative method is to write a short BASIC program for the purpose as described below.

Each of the keys (Shift+F1 to Shift+F8, and F9 to F12) has a corresponding entry in the Registry, as follows:

HKEY_CURRENT_USER\Software\R T Russell\BBC BASIC for Windows\Macros\F1
HKEY_CURRENT_USER\Software\R T Russell\BBC BASIC for Windows\Macros\F2
HKEY_CURRENT_USER\Software\R T Russell\BBC BASIC for Windows\Macros\F3
HKEY_CURRENT_USER\Software\R T Russell\BBC BASIC for Windows\Macros\F4
HKEY_CURRENT_USER\Software\R T Russell\BBC BASIC for Windows\Macros\F5
HKEY_CURRENT_USER\Software\R T Russell\BBC BASIC for Windows\Macros\F6
HKEY_CURRENT_USER\Software\R T Russell\BBC BASIC for Windows\Macros\F7
HKEY_CURRENT_USER\Software\R T Russell\BBC BASIC for Windows\Macros\F8
HKEY_CURRENT_USER\Software\R T Russell\BBC BASIC for Windows\Macros\F9
HKEY_CURRENT_USER\Software\R T Russell\BBC BASIC for Windows\Macros\F10
HKEY_CURRENT_USER\Software\R T Russell\BBC BASIC for Windows\Macros\F11
HKEY_CURRENT_USER\Software\R T Russell\BBC BASIC for Windows\Macros\F12

When one of the keys is pressed the corresponding Registry entry is checked, and if present the macro string stored there is executed.

The string associated with a particular macro can be of any (reasonable) length and contains the usual 8-bit (ANSI) character codes corresponding to the required sequence of key-presses. Any character which can be obtained from the keyboard (including those which require the Shift and/or Ctrl keys to be pressed) may be included in the string. However, character codes which cannot be directly obtained from the keyboard must not be included.

So to program the F12 key to generate Hello world! one could use the following simple program:

        Macro12$ = "Hello world!"
 
        Key$ = "Software\R T Russell\BBC BASIC for Windows\Macros"
        SYS "RegCreateKeyEx", &80000001, Key$, 0, "", 0, &F003F, 0, ^K%, ^D% TO R%
        IF R% = 0 THEN
          SYS "RegSetValueEx", K%, "F12", 0, 3, Macro12$, LEN(Macro12$)
          SYS "RegCloseKey", K%
        ENDIF

Of course there are several keyboard keys which do not generate characters, but which either modify the action of other keys (e.g. Shift, Ctrl, Alt) or have special actions such as cursor movement or Escape. The function keys themselves are in this category. It is possible to incorporate these keys into a macro string using the special values CHR$0 or CHR$255 followed immediately by the Virtual Key code of the key concerned. In the first case (CHR$0+VKcode$) the effect is to press the (virtual) key and in the second (CHR$255+VKcode$) to release it.

So to program the F11 key so that it alternately adds and removes line numbers to the current program you can do the following:

        Alt_Down$ = CHR$0+CHR$18
        Alt_Up$ = CHR$255+CHR$18
        Enter$ = CHR$13
 
        Macro11$ = Alt_Down$+"URR"+Alt_Up$+Enter$
 
        Key$ = "Software\R T Russell\BBC BASIC for Windows\Macros"
        SYS "RegCreateKeyEx", &80000001, Key$, 0, "", 0, &F003F, 0, ^K%, ^D% TO R%
        IF R% = 0 THEN
          SYS "RegSetValueEx", K%, "F11", 0, 3, Macro11$, LEN(Macro11$)
          SYS "RegCloseKey", K%
        ENDIF

This corresponds to the following sequence of keyboard operations:

  1. Hold down the Alt key
  2. Press U (open the Utilities menu)
  3. Press R (select the Renumber menu item)
  4. Press R (toggle the 'Remove unused line numbers' option)
  5. Release the Alt key
  6. Press Enter (activate the OK button)


Note that you should always use the press and release codes in pairs, so the macro leaves the 'virtual keyboard' in the same state that it found it.

You can even create a macro that executes a BASIC program, by generating a sequence of keypresses which will enter 'immediate mode' and run the program from there:

        Alt_Down$ = CHR$0+CHR$18
        Alt_Up$ = CHR$255+CHR$18
        Enter$ = CHR$13
 
        Macro1$ = Alt_Down$+"RI"+Alt_Up$+"CALL @lib$+""..\examples\tools\searchbbc"""+Enter$
 
        Key$ = "Software\R T Russell\BBC BASIC for Windows\Macros"
        SYS "RegCreateKeyEx", &80000001, Key$, 0, "", 0, &F003F, 0, ^K%, ^D% TO R%
        IF R% = 0 THEN
          SYS "RegSetValueEx", K%, "F1", 0, 3, Macro1$, LEN(Macro1$)
          SYS "RegCloseKey", K%
        ENDIF

The corresponding keyboard sequence is as follows:

  1. Hold down the Alt key
  2. Press R (open the Run menu)
  3. Press I (select the Immediate Mode menu item)
  4. Release the Alt key
  5. Enter the command “CALL @lib$+”..\examples\tools\searchbbc“”
  6. Press Enter


Note that the program is executed using CALL rather than CHAIN (or RUN) so that the existing program in memory, if any, is unaffected. However, care must be taken when writing programs that are intended to be run this way (for example any procedures and functions they contain must have different names from those in the currently-loaded program).

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
creating_20function_20key_20macros.txt · Last modified: 2024/01/05 00:22 by 127.0.0.1