INP: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "INP returns a value from a computer register or port values at a specified physical address. {{PageSyntax}} : {{Parameter|i}} = INP({{Parameter|address}}) * '''QB64 has limited access to registers. VGA memory and registers are emulated.''' * Address can be a decimal or hexadecimal INTEGER value. * INP reads directly from a register or port address. * It does not require a DEF SEG memory segment address like PEEK or POKE do. * Reads color p...")
 
No edit summary
Line 15: Line 15:
{{PageExamples}}
{{PageExamples}}
''Example 1:'' Reading the current RGB color settings used in a bitmap to an array.
''Example 1:'' Reading the current RGB color settings used in a bitmap to an array.
{{CodeStart}} '' ''
{{CodeStart}}
  SCREEN 12
  SCREEN 12
  {{Cl|DIM}} Colors%(47)
  {{Cl|DIM}} Colors%(47)
Line 21: Line 21:
  {{Cl|FOR...NEXT|FOR}} i = 0 {{Cl|TO}} 47
  {{Cl|FOR...NEXT|FOR}} i = 0 {{Cl|TO}} 47
  Colors%(i) = {{Cl|INP}}(&H3C9) ' moves to next color attribute every 3 loops
  Colors%(i) = {{Cl|INP}}(&H3C9) ' moves to next color attribute every 3 loops
  {{Cl|NEXT}} '' ''
  {{Cl|NEXT}}
{{CodeEnd}}
{{CodeEnd}}




''Example 2:'' Reading the keyboard Scan Codes as an alternative to [[INKEY$]]
''Example 2:'' Reading the keyboard Scan Codes as an alternative to [[INKEY$]]
{{CodeStart}} '' ''
{{CodeStart}}
  {{Cl|DO}}: {{Cl|SLEEP}}
  {{Cl|DO}}: {{Cl|SLEEP}}
     scancode% = {{Cl|INP}}(&H60)
     scancode% = {{Cl|INP}}(&H60)
     a$ = {{Cl|INKEY$}} ' clears keyboard buffer
     a$ = {{Cl|INKEY$}} ' clears keyboard buffer
     {{Cl|PRINT}} scancode%;  
     {{Cl|PRINT}} scancode%;
  {{Cl|LOOP}} {{Cl|UNTIL}} scancode% = 1 ' [ESC] keypress exit '' ''
  {{Cl|LOOP}} {{Cl|UNTIL}} scancode% = 1 ' [ESC] keypress exit
{{CodeEnd}}
{{CodeEnd}}




''Example 3:'' A simple ping pong game using an array function to read multiple keys for two players.
''Example 3:'' A simple ping pong game using an array function to read multiple keys for two players.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|DEFINT}} A-Z
{{Cl|DEFINT}} A-Z
{{Cl|SCREEN}} 12
{{Cl|SCREEN}} 12
Line 101: Line 101:
K$ = {{Cl|INKEY$}}
K$ = {{Cl|INKEY$}}
ScanKey% = keyflags%(scancode%)
ScanKey% = keyflags%(scancode%)
{{Cl|END FUNCTION}} '' ''
{{Cl|END FUNCTION}}
{{CodeEnd}}
{{CodeEnd}}
: ''Note:'' [[_KEYDOWN]] can be used to read multiple keys simultaneously and is the '''recommended practice'''.
: ''Note:'' [[_KEYDOWN]] can be used to read multiple keys simultaneously and is the '''recommended practice'''.




{{PageSeeAlso}}  
{{PageSeeAlso}}
* [[OUT]] {{text|(write to register)}},  [[PEEK]] {{text|(read memory)}}
* [[OUT]] {{text|(write to register)}},  [[PEEK]] {{text|(read memory)}}
* [[INKEY$]], [[_KEYHIT]], [[_KEYDOWN]]
* [[INKEY$]], [[_KEYHIT]], [[_KEYDOWN]]

Revision as of 01:49, 23 January 2023

INP returns a value from a computer register or port values at a specified physical address.


Syntax

i = INP(address)


  • QB64 has limited access to registers. VGA memory and registers are emulated.
  • Address can be a decimal or hexadecimal INTEGER value.
  • INP reads directly from a register or port address.
  • It does not require a DEF SEG memory segment address like PEEK or POKE do.
  • Reads color port intensity settings after OUT &H3C7, attribute sets the starting attribute read mode.


Examples

Example 1: Reading the current RGB color settings used in a bitmap to an array.

 SCREEN 12
 DIM Colors%(47)
 OUT &H3C7, 0 ' set color port for INP reads at attribute 0 to start
 FOR i = 0 TO 47
 Colors%(i) = INP(&H3C9) ' moves to next color attribute every 3 loops
 NEXT


Example 2: Reading the keyboard Scan Codes as an alternative to INKEY$

 DO: SLEEP
    scancode% = INP(&H60)
    a$ = INKEY$ ' clears keyboard buffer
    PRINT scancode%;
 LOOP UNTIL scancode% = 1 ' [ESC] keypress exit


Example 3: A simple ping pong game using an array function to read multiple keys for two players.

DEFINT A-Z
SCREEN 12
DIM ball%(100)        ' Set aside enough space to hold the ball sprite
CIRCLE (4, 3), 4, 4
PAINT (4, 3), 12, 4   ' Draw a filled circle and fill for ball
GET (0, 0)-(8, 7), ball%(0) ' Get the sprite into the Ball% array

begin:
xmin = 10: ymin = 10
xmax = 630: ymax = 470
x = 25: y = 25
dx = 1: dy = 1
LTpos = 50: RTpos = 50

DO: _LIMIT 100 'adjust higher for faster
CLS
IF ScanKey%(17) THEN LTpos = LTpos - 1
IF ScanKey%(31) THEN LTpos = LTpos + 1
IF ScanKey%(72) THEN RTpos = RTpos - 1
IF ScanKey%(80) THEN RTpos = RTpos + 1

PRINT "Player 1 : "; ponescore; " Player 2 : "; ptwoscore

IF x > xmax - 15 AND y >= RTpos AND y <= RTpos + 100 THEN
dx = -1
ELSEIF x > xmax THEN
ponescore = ponescore + 1
GOSUB begin
END IF

IF x < xmin + 15 AND y >= LTpos AND y <= LTpos + 100 THEN
dx = 1
ELSEIF x < xmin THEN
ptwoscore = ptwoscore + 1
GOSUB begin
END IF

IF y > ymax - 5 THEN dy = -1
IF y < ymin + 5 THEN dy = 1
' Display the sprite elsewhere on the screen

x = x + dx
y = y + dy

PUT(x, y), ball%(0)


LINE (20, LTpos)-(20, LTpos + 100)
LINE (620, RTpos)-(620, RTpos + 100)

_DISPLAY 'shows completed screen every call

LOOP UNTIL ScanKey%(1)
END


FUNCTION ScanKey% (scancode%)
STATIC Ready%, keyflags%()
IF NOT Ready% THEN REDIM keyflags%(0 TO 127): Ready% = -1
i% = INP(&H60) 'read keyboard states
IF (i% AND 128) THEN keyflags%(i% XOR 128) = 0
IF (i% AND 128) = 0 THEN keyflags%(i%) = -1
K$ = INKEY$
ScanKey% = keyflags%(scancode%)
END FUNCTION
Note: _KEYDOWN can be used to read multiple keys simultaneously and is the recommended practice.


See also


External Links



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link