Difference between revisions of "Raw key events"
(Replaced <pre> by <code>) |
|||
Line 3: | Line 3: | ||
==Detecting keystrokes== | ==Detecting keystrokes== | ||
In case I made you think the os sucks, it doesn't (all the time). Because this os is not Windows, we can access lower-level functions, such as os.pullEvent(). And that's the one that we're going to use: | In case I made you think the os sucks, it doesn't (all the time). Because this os is not Windows, we can access lower-level functions, such as os.pullEvent(). And that's the one that we're going to use: | ||
− | < | + | <pre> |
function rawread() | function rawread() | ||
bRead = true | bRead = true | ||
Line 13: | Line 13: | ||
end | end | ||
end | end | ||
− | </ | + | </pre> |
This function waits for any key, then exits. | This function waits for any key, then exits. | ||
==Figuring out the key== | ==Figuring out the key== | ||
We can detect any key, but not which key. To find it out, we have to add some further logic to the code: | We can detect any key, but not which key. To find it out, we have to add some further logic to the code: | ||
− | < | + | <pre> |
function rawread() | function rawread() | ||
bRead = true | bRead = true | ||
Line 31: | Line 31: | ||
end | end | ||
end | end | ||
− | </ | + | </pre> |
This function waits for the Enter key to be pressed. If so, it writes "Enter detected" to the screen and exits. | This function waits for the Enter key to be pressed. If so, it writes "Enter detected" to the screen and exits. | ||
Revision as of 18:00, 12 April 2012
The built-in os has a function to read incoming keystrokes. But it also implements the functions to move the cursor while typing. This is not always a desired feature; imagine a case in which you want to make a menu with a controllable cursor around the menu entries. The current read() function won't let you.
Detecting keystrokes
In case I made you think the os sucks, it doesn't (all the time). Because this os is not Windows, we can access lower-level functions, such as os.pullEvent(). And that's the one that we're going to use:
function rawread() bRead = true while(bRead) local sEvent, param = os.pullEvent("key") if(sEvent == "key") then break end end end
This function waits for any key, then exits.
Figuring out the key
We can detect any key, but not which key. To find it out, we have to add some further logic to the code:
function rawread() bRead = true while(bRead) local sEvent, param = os.pullEvent("key") if(sEvent == "key") then if(param == 28) then print("Enter detected") break end end end end
This function waits for the Enter key to be pressed. If so, it writes "Enter detected" to the screen and exits.
Key scan codes
The scan codes are typical DirectX numbers.
|
|
|
|
|
Source: Nexus Wiki
Author(s)
They deserve credit!
- Initial page - Wukl