Got Keys? (RBD3212)
Most keyboard handling in REALbasic is synchronous; that is, you wait for a key-related event to occur, and you handle it without knowing whether or not the key has been released by the time your code executes. However, if you’re modifying the behavior of a standard control, or programming a custom control or a game, you might need to know whether a key is pressed at the moment your code is executing, without waiting for the next keyboard event. That’s called asynchronous keyboard input. A common example is using the Shift key to constrain a drag to a horizontal or vertical axis. REALbasic lets us check the status of all Shift keys as a group, using the built-in Keyboard. AsyncShiftKey function. There are similar functions for the other common modifier keys.
But what if you want to see if only the left or right Shift key is down, or check the status of an arbitrary key on the keyboard? This kind of thing can get tricky if you want your code to run on any operating system, or even on different keyboard layouts within a single operating system.
In this article, we’ll devise code that we can drop into any project to access the keyboard easily, efficiently, and asynchronously, in a platform-neutral way. How cool is that?