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

Getting a single character from the keyboard

Question

I'm trying to make a function that will wait until you type a character, then return that character. However, the code I currently have waits until I press a key, then returns that key. Which isn't quite what I want. If I try to type a capital A, I don't want it to return 'left shift'; I want it to return 'A'. Is there a built-in function for this I'm missing, or someone who can point me to sample code?

 

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0
  • Solution

You could try checking on the keypress if shift is down then use string.upper(char)

 

Like this:

if keyboard.isShiftDown() then
    --Shift pressed.
    char = string.upper(char)
end
print(char)

Hope this helps :)

 

--EDIT:

After reading your post again it looks like your code will detect the pressing of shift as a keypress and print the result anyway. You'll have to account for that.

 

Maybe like this:

while true do
    ev, p1, p2, p3, p4, p5 = event.pull(.01, _, ev, p1, p2, p3, p4, p5)
    if ev == "key_down" then
        local char = keyboard.keys[p3]
        if char == "left_shift" or char == "right_shift" then
        else
            if keyboard.isShiftDown() then
                --Shift pressed.
                char = string.upper(char)
            end
            print(char)
        end
    end
end

I think p3 is the charachter, I'm not sure and I don't really want to check right now.

Let me know if you have any trouble.

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.