'===========================================================================
' Subject: SIMULATE KEYPRESS                  Date: 07-06-98 (12:47)       
'  Author: Egbert Zijlema                     Code: PB                     
'  Origin: alt.lang.powerbasic              Packet: KEYBOARD.ABC
'===========================================================================
'> Anyway, I know how to put regular characters in the keyboard buffer.
'> but unfortunately this does not allow to send along extended
'> key information...
'> So I can send "X", but don't know how to send ALT-X...
'> 
'> Any ideas?
'--------------------

' STUFF.BAS  - simulate keypress
' Author: Egbert Zijlema (e.zylema@castel.nl)
' Status: Public Domain / freeware
' Code  : PowerBASIC

DEFINT A-Z

' name constants for registers
  %AX = 1
  %CX = 3

' name constants for demo keys
  %ALTX = 45 * 256                      ' Alt-x
  %ESC  = 27                            ' Escape
  %F1   = 59 * 256                      ' F1
  %A    = 65                            ' A (uppercase)

FUNCTION GetKey() AS INTEGER
  ' function to test the result of StuffBuff
  ' returns keystroke from buffer in the following format:
  '   - ascii code when typewriter key
  '   - 256 times scancode when extended key

  DO
  LOOP UNTIL INSTAT
  FUNCTION = CVI(INKEY$ + CHR$(0))
END FUNCTION

SUB StuffBuff(KeyCode AS WORD)
  ' procedure to simulate keypress
  ' KeyCode must be passed as either a valid ASCII or
  ' (256 times scancode) for extended keys

  REG %AX, &H0500              ' service 05h to AX
  REG %CX, KeyCode             ' key value to CX
  CALL INTERRUPT &H16          ' call keyboard interrupt
END SUB

' demo
CLS
  StuffBuff %ALTX              ' simulate pressing Alt-x
  ? "Scancode of Alt-x ="; GetKey \ 256
  
  StuffBuff %F1                
  ? "Scancode of F1 ="; GetKey \ 256
  
  StuffBuff %ESC               
  ? "ASCII code of Escape ="; GetKey

  StuffBuff %A
  ? CHR$(GetKey)
END
