Window.KeyDown

From Xojo Documentation

Event


Window.KeyDown(Key as String) As Boolean

Supported for all project types and targets.

The passed Key has been pressed and not handled by an object in the window. For example, The tab key is never sent to any control. It is instead handled by the window itself. If the window has no controls that can receive the focus, any keys that are pressed will generate KeyDown events for the window. This event handler is passed a parameter that tells you which key was pressed.

Returns a Boolean. Returning True means that no further processing is to be done with the Key, although the KeyUp event is still called.

Example

The following example scrolls a picture. The picture has been added to the project. The properties XScroll and YScroll have been added to the window to hold the amounts the picture has been scrolled.

A convenient way to scroll a picture is with the four arrow keys. To do this, you place code in the KeyDown event handler of the window. This event receives each keystroke. Your code can test whether any of the arrow keys have been pressed and then take the appropriate action. For example, this code in the KeyDown event of the window scrolls the picture 8 pixels at a time:

Function KeyDown (Key As String) As Boolean
Select Case Asc(Key)
Case 31 // up arrow
Yscroll = YScroll - 8
Canvas1.Scroll(0, -8)
Case 29 // Right arrow
Xscroll = XScroll - 8
Canvas1.Scroll(-8, 0)
Case 30 // Down arrow
YScroll = YScroll + 8
Canvas1.Scroll(0, 8)
Case 28 // Left arrow
XScroll = XScroll + 8
Canvas1.Scroll(8, 0)
End Select

The Paint event of the Canvas has the line of code that draws the picture:

g.DrawPicture(myPicture, XScroll, YScroll)