BB___Inkey

=Inkey$= toc

Description
> This special variable holds the value of keyboard keypresses, called Virtual Key Codes. Key strokes can only be captured when a graphic window or graphicbox has the input focus.

Syntax

 * 1)  - (case sensitive)

Hints
>> code format="lb" code >> code format="lb" keyPressed$ = upper$(Inkey$) if keyPressed$ = "A" then print "A was typed." code >> code format="lb" if len(Inkey$) > 1 then print "Special key was pressed." code
 * //**Setting Focus.**// To be certain your graphic window or graphicbox has the focus, issue a **"setfocus"** command to the window.
 * //**Trapping Keypresses.**// Keypresses are captured using the command "when characterInput" and the value contained in Inkey$ is evaluated by the code located in the "eventHandler" branch label or subroutine. An example:
 * 1) handle.ext "setfocus; when characterInput eventHandler"
 * //**Testing for Case.**// If you are testing for a key press that must match either an uppercase or lowercase character, modify the Inkey$ assignment with the upper$ or lower$ function:
 * //**Testing for One or Multi-Character Codes.**// To test if Inkey$ holds more than one character, use the **len**function:

Evaluating Inkey$
>> code format="lb" [checkKey] keyCode$=Inkey$ if asc(right$(keyCode$, 1)) = _VK_UP then print "Up arrow was pressed" wait code > To determine whether the Shift, Ctrl, or Alt key has been pressed, use the bitwise AND operator to compare the ASCII value of the first character in Inkey$ AND the ASCII value of the particular key (or combination of keys). Here's a snippet of code to illustrate: >> code format="lb" [check] shift=4 ctrl=8 alt=16 a=asc(left$(Inkey$,1)) if len(Inkey$)>1 then m$="" if a AND shift then m$="shift " if a AND ctrl then m$=m$+"ctrl " if a AND alt then m$=m$+"alt " print "Special keys pressed: " + m$  else print "Key pressed: " + Inkey$ end if wait code
 * //**Normal Printable Characters.**// If Inkey$ holds a single character, it will be the key that one would normally press to display a printable character to the screen. These would be the characters that are printed on the keys and are typed without pressing the shift key.
 * //**Non-Printable Characters.**// If Inkey$ holds more than one character, it may be a '//shifted//' printable character or the code for a special key such as Tab, Caps Lock, arrow keys, F-keys, Delete, Insert, etc. The special keys are always represented by two characters, and the ASCII value of the second character is the code for that special key. These keys are often represented by codes called Windows Constants - e.g.: _VK_SHIFT; _VK_F1; _VK_UP; etc. To see if the Up arrow key was pressed, for example, use this code:
 * //**Shift, Ctrl, Alt keys.**// Inkey$ can hold a two-character code when any of these keys, or any combination of these keys, is pressed. This allows you to capture and evaluate all of the keys (characters) that are accessible when the shift key is held down, for example. And it allows the use of key combinations such as Ctrl-A, or Alt-X in your program.
 * //**Key Up and Key Down.**// Special keys trigger a new value for Inkey$ when they are pressed and again when they are released.

> A partial list of Virtual Key Codes can be found in the JB Help file "Using virtual key constants with Inkey$". A complete list can be found at the MSDN Library in the topic Virtual-Key Codes.

Example
A sample program from JB Help file. code format="lb" 'INKEY.BAS - how to use the Inkey$ variable

open "Inkey$ example" for graphics as #graph print #graph, "when characterInput [fetch]"

[mainLoop] print #graph, "setfocus" input r$

[fetch] 'a character was typed!

key$ = Inkey$ if len(key$) = 1 then notice key$+" was pressed!"

else keyValue = asc(right$(key$, 1)) if keyValue = _VK_SHIFT then notice "Shift was pressed" else if keyValue = _VK_CONTROL then notice "Ctrl was pressed" else notice "Unhandled key pressed" end if     end if  end if

wait code

Useful Functions
code format="lb" Place a useful function using this command here code