DesktopWindow.KeyDown

From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!
Event


DesktopWindow.KeyDown(Key as String) As Boolean

New in 2021r3

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:

Select Case Key.Asc
Case 31 // down arrow
YScroll = YScroll - 8
Canvas1.Scroll(0, -8)
Return True
Case 29 // Right arrow
XScroll = XScroll - 8
Canvas1.Scroll(-8, 0)
Return True
Case 30 // up arrow
YScroll = YScroll + 8
Canvas1.Scroll(0, 8)
Return True
Case 28 // Left arrow
XScroll = XScroll + 8
Canvas1.Scroll(8, 0)
Return True
End Select

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

g.DrawPicture(MyPicture, XScroll, YScroll)