Q(uick)BASIC Function: PEEK
Quick View
PEEK
A memory function that returns the byte stored at a specified memory location
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- PEEK(address)
- POKE address,byte%
Description/Parameter(s)
address | A byte position relative to the current segment address set by DEF SEG; a value in the range 0 through 65,535. |
byte% | A byte value to write to the specified memory location; a value in the range 0 through 255. |
Example
DEF SEG = 0
Status% = PEEK(&H417) 'Read keyboard status.
POKE &H417, (Status% XOR &H40) 'Change Caps Lock state, bit 6.
See also:
Syntax
- PEEK(address)
Description/Parameter(s)
The returned value is an integer in the range 0-255. The argument address is a value in the range 0-65,535. The argument address is treated as the offset from the current default segment (as set by the DEF SEG statement).
If the argument is a single- or double-precision floating-point value or a long integer, it is converted to a two-byte integer.
The PEEK function complements the POKE statement.
Example
See the ⮜ DEF SEG statement programming example ⮞, which uses the PEEK statement.
See also:
Syntax
- PEEK(address)
Description/Parameter(s)
address | The offset from the current default segment (as set by the DEF SEG statement). The value of address is between 0 and 65,535, inclusive. |
Usage Notes
- If address is a single- or double-precision floating-point value or a long integer, it is converted to a 2-byte integer.
- The PEEK function complements the POKE statement.
- When using PEEK to return a byte from a far-string array, use the SSEG and SADD functions to obtain the current segment and offset. For example:
- ' Set the current segment address to the address of a$.
- DEF SEG = SSEG(a$)
- ' Determine the string's location within the segment.
- StringOffset = SADD(a$)
- ' Return the byte stored at this location.
- PEEK(StringOffset)
Important
- Direct string manipulation with PEEK should be used cautiously, because BASIC moves string locations during runtime. The DEF SEG statement should be executed immediately before using the PEEK function.
PEEK and Expanded-Memory Arrays
- Do not use PEEK to return a byte from an expanded memory array. If you start QBX with the /Ea switch, any of these arrays may be stored in expanded memory:
- Numeric arrays < 16K in size
- Fixed-length string arrays < 16K in size
- User-defined-type arrays < 16K in size
- If you want to use PEEK to return a byte from an array, first start QBX without the /Ea switch. (Without the /Ea switch, no arrays are stored in expanded memory.)
- For more information on using expanded memory, see ⮜ Using Expanded Memory ⮞.
Programming With OS/2 Protected Mode
- Any address referred to by PEEK must be readable. If PEEK refers to an address for which your process does not have read permission, the operating system may generate a protection exception, or BASIC may generate the error message, "Permission denied."
Example
This example uses the DEF SEG statement, the PEEK function, and POKE statement to turn the Caps Lock key on and off. The program uses the LINE INPUT statement to prompt the user to enter a string, demonstrating that the Caps Lock key is on.
Note: This program contains hardware-specific instructions. It works correctly on IBM PC, XT, and AT computers.
DECLARE SUB CapsOn ()
DECLARE SUB CapsOff ()
DECLARE SUB PrntMsg (R%, C%, M$)
CLS
CapsOn
PrntMsg 24, 1, "<Caps Lock On>"
LOCATE 12, 10
LINE INPUT "Enter a string (all characters are caps): ", S$
CapsOff
PrntMsg 24, 1, " "
PrntMsg 25, 1, "Press any key to continue..."
DO WHILE INKEY$ = "": LOOP
CLS
END
SUB CapsOff STATIC
'Turn Caps Lock off.
DEF SEG = 0
'Set Caps Lock off (turn off bit 6 of &H0417).
POKE &H417, PEEK(&H417) AND &HBF
DEF SEG
END SUB
SUB CapsOn STATIC
'Turn Caps Lock on.
'Set segment to low memory.
DEF SEG = 0
'Set Caps Lock on (turn on bit 6 of &H0417).
POKE &H417, PEEK(&H417) OR &H40
'Restore segment.
DEF SEG
END SUB
SUB PrntMsg (Row%, Col%, Message$) STATIC
'Print message at Row%, Col% without changing cursor.
'Save cursor position.
CurRow% = CSRLIN: CurCol% = POS(0)
LOCATE Row%, Col%: PRINT Message$;
'Restore cursor.
LOCATE CurRow%, CurCol%
END SUB