Difference between revisions of "Raw key events"

From ComputerCraft Wiki
Jump to: navigation, search
(added keymapper api reference)
(Added keyboard-image, need to add table version.)
 
(13 intermediate revisions by 10 users not shown)
Line 2: Line 2:
  
 
==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>
 
<pre>
 
function rawread()
 
function rawread()
Line 20: Line 20:
 
<pre>
 
<pre>
 
function rawread()
 
function rawread()
    bRead = true
+
     while true do
     while(bRead) do
+
 
         local sEvent, param = os.pullEvent("key")
 
         local sEvent, param = os.pullEvent("key")
         if(sEvent == "key") then
+
         if sEvent == "key" then
             if(param == 28) then
+
             if param == 28 then
 
                 print("Enter detected")
 
                 print("Enter detected")
 
                 break
 
                 break
Line 33: Line 32:
 
</pre>
 
</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.
 
However, this is only good for checking single keys. Also, what if you don't really want to look up the scan code on this page every time you code?
 
Fortunately there is a solution: the [[Raw key events/keymapper|keymapper API]]. See page for details.
 
  
 
==Key scan codes ==
 
==Key scan codes ==
 +
[[File:CC-Keyboard-Charcodes.png|center|frame|250x250px|Click for a larger copy of this image - contains keys and their event numbers.]]
 +
 +
<!--
 
The scan codes are typical DirectX numbers.
 
The scan codes are typical DirectX numbers.
 
{|
 
{|
Line 57: Line 56:
 
| 3
 
| 3
 
| 4
 
| 4
 +
|-
 +
| 4
 +
| 5
 
|-
 
|-
 
| 5
 
| 5
 
| 6
 
| 6
 +
|-
 +
| 6
 +
| 7
 
|-
 
|-
 
| 7
 
| 7
 
| 8
 
| 8
 +
|-
 +
| 8
 +
| 9
 
|-
 
|-
 
| 9
 
| 9
Line 311: Line 319:
 
|-
 
|-
 
| Numpad Enter
 
| Numpad Enter
| 156
+
| 28 <s>156</s> *
 
|-
 
|-
 
| Right Control
 
| Right Control
Line 358: Line 366:
 
| 211
 
| 211
 
|-
 
|-
| Left Mouse Button
+
| Left Super
| 256
+
| 219
 
|-
 
|-
| Right Mouse Button
+
| Right Super
| 257
+
| 220
 
|-
 
|-
| Middle Mouse/Wheel
+
| Menu
| 258
+
| 221
 
|-
 
|-
| Mouse Button 3
 
| 259
 
|-
 
| Mouse Button 4
 
| 260
 
|-
 
| Mouse Button 5
 
| 261
 
|-
 
| Mouse Button 6
 
| 262
 
|-
 
| Mouse Button 7
 
| 263
 
|-
 
| Mouse Wheel Up
 
| 264
 
|-
 
| Mouse Wheel Down
 
| 265
 
 
|}
 
|}
 
|}
 
|}
Source: [http://wiki.tesnexus.com/index.php/DirectX_Scancodes_And_How_To_Use_Them Nexus Wiki]
+
Source: [http://wiki.tesnexus.com/index.php/DirectX_Scancodes_And_How_To_Use_Them Nexus Wiki]<br />
 
+
<i>Note: Items marked with * are not the correct keycode, but return a matching keycode for another button (eg, enter keys).</i>-->
====Author(s)====
+
They deserve credit!
+
*Initial page - [[User:Wukl|Wukl]]
+
  
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]

Latest revision as of 22:05, 21 December 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) do
        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()
    while true do
        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

Click for a larger copy of this image - contains keys and their event numbers.