Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Ch1ck3nL1v3r

Some keyboard keys are "0"

Question

Hello! I am trying to create my own keyboard library, but I have hit a problem in that some of the keys on my keyboard always return 0 when they are pressed. These keys are the "\" and "#" key on my UK layout keyboard.

 

Here is my test code:

while true do
  local _, _, _, key = event.pull("key_down")
  print(key)
end

This always returns 0 when backslash or hash is pressed. If anybody knows why / how I can distinguish between those two keys that would be awesome.

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

I think this is because of the library that Minecraft uses to read keyboard input, which doesn't work properly with certain keys on non-US keyboards. On my German keyboard for example, the < key (which I think is where your \ key is) registers as "NONE" in the Minecraft controls options. The same goes for the key codes from the key event. IIRC the key event also includes the typed character in one of the other event data values, which should match the actual keyboard layout, maybe you can check against that.

Link to post
Share on other sites
  • 0

Well, OpenComputers supports non-US keyboards!

OpenOS has a library for that ;)

 

Let me explain that:

Have a look at this code:

local event, address, char, code, userName = computer.pullSignal()

so the event varable can be "key_down" or "key_up" or "touch", for example.

address is your Keyboard address, that is not really helpful for us.

The code and char varables are really important, and userName, yes not really important.

 

The code and char are the varaibles that we must to use.

Ok, have a look on `code`.

these are 'Minecraft' key-codes.

For example:

Key 'a' is 0x1E or 30.

For a full list, show the full keyboard List on GitHub: https://github.com/MightyPirates/OpenComputers/blob/master-MC1.7.10/src/main/resources/assets/opencomputers/loot/openos/lib/tools/keyboard_full.lua

 

 

So and now to the important part, the `char` value.

The `char` value get 'all' chars-code that you have on your keyboard!

But the problem is, we can get some control chars.

We can't use them dierectly.

 

To avoid this problem we can make a if statement:

if not type(char) ~= "number" and (char >= 0x20 or (char <= 0x7F and char >= 0x9F)) then
  -- our code here...
end

Ok, we make sure we have a 'printable char' and now?

The variable `code` neither the variable `char` give you a correct 'char'.

 

To get a right char, you need:

unicode.char(charcode)

So your code may look like so:

while true do
  local event = {computer.pullSignal()}; -- wait for an input...
  if not type(event[3]) ~= "number" and (event[3] >= 0x20 or (event[3] <= 0x7F and event[3] >= 0x9F)) then -- if a printable char was pressed...
    print(unicode.char(event[3])) -- print the correct char...
  end
end

I hope that was a little help to you ;)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.