How to correctly read text from keyboard

General discussion about LÖVE, Lua, game development, puns, and unicorns.
cristoferfb
Prole
Posts: 16
Joined: Wed May 15, 2019 12:41 am
Location: Valparaiso, Chile.

How to correctly read text from keyboard

This is my class diagram:

and this is my code:

Code: Select all

require "views/functionView"

functionView = FunctionView()
end

function love.draw ()
functionView:draw()
end

function love.textinput (text)
-- ???
end

function love.update (dt)
functionView:update(dt)
end
My question is ¿How I can read text from my Node class? As u can see it is at three levels of abstraction inside my FuncionView Class, and the love.textinput event handle is in the top of my file tree. ¿Any scalable way for do it?

EDIT:

As the title say I want to read text from keyboard, not how to read a key, I need emulate the behaviour of a input text in Android. Then, as u can see in the example code, I read about the love.textinput event handler but the class thats need it is inside of 2 parents class, so I obviusly can break some paradigms for take it more easy, but speaking of real projects with a certain complexity that needs order and nice practices, there is a relatively clean way of do it?
Last edited by cristoferfb on Sun May 10, 2020 8:35 am, edited 4 times in total.

4vZEROv
Citizen
Posts: 93
Joined: Wed Jan 02, 2019 8:44 pm

Re: How to correctly read text from keyboard

What do you want to do ?
What do your classes represent ?

To respond to the title of your post :
Callback :
https://love2d.org/wiki/love.keypressed

Inside love.update:
https://love2d.org/wiki/love.keyboard.isDown

cristoferfb
Prole
Posts: 16
Joined: Wed May 15, 2019 12:41 am
Location: Valparaiso, Chile.

Re: How to correctly read text from keyboard

4vZEROv wrote:
Sun May 10, 2020 1:57 am

What do you want to do ?
What do your classes represent ?

To respond to the title of your post :
Callback :
https://love2d.org/wiki/love.keypressed

Inside love.update:
https://love2d.org/wiki/love.keyboard.isDown
Sorry for not be clear I reedit my question, I think not is importat know what represent any class, but for example, with the functions u show me, with love.keyboard.isDown, I can easily use that inside any method of any class for read a speciffic key, but how I can do the same for read full words (text input)?

tobiasvl
Prole
Posts: 29
Joined: Mon Oct 01, 2018 4:58 pm
Location: Norway
Contact:

Re: How to correctly read text from keyboard

Can you explain your problem better? What exactly is the problem? How does Android do text input, since you want to do it the way Android does it? Note that if you're making this for Android, text input is disabled by default and you need to call love.keyboard.setTextInput to enable it (which will display the on-screen keyboard).

What is the Node class and what do you mean you want to read text "from" your Node class? What have you tried so far and how is it not working?

There's no callback or function to read full words in LÖVE, so you need to stitch together the input, like the example on the love.textinput wiki page says. (It might actually buffer input so you get more than one character at a time, I have no idea really, but it doesn't give you whole words, at any rate.)

cristoferfb
Prole
Posts: 16
Joined: Wed May 15, 2019 12:41 am
Location: Valparaiso, Chile.

Re: How to correctly read text from keyboard

Well the important part of the classes I mentioned is understand that FunctionView instantiate a child class called Path and that it own child class called Node (basic object oriented concepts, no more deep here) as u can see in my code example the callback 'love.textinput ()' is in a different level of abstraction vs the Node class (this is the only importance of the class diagram I showed previusly, ilustrate the position of my Node respect the main class FunctionView) so if u say me that is the only way of read full text... How does a real project handle all the 'text input' calls it might need using only that callback? Think I may have different UI components (in different .lua files) that may need use text input. I am asking more for a nice practice or advice about this.

Ref
Party member
Posts: 690
Joined: Wed May 02, 2012 11:05 pm

Re: How to correctly read text from keyboard

I don't know about "correct" but I've hacked a library together that allows test input without too much of a burden.
Sure you will come up with something better but might get some ideas from this.
Best
Attachments
EnterText.love
Simple library of questionable code

zorg
Party member
Posts: 2995
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to correctly read text from keyboard

Since i am assuming only one UI component would ever be active, you just have an appropriate method in all relevant components, that can access whatever the textinput callback got the last frame, by storing that somewhere in the love.textinput callback. You then move that data into the active component's text buffer. The components would have their own buffer so it would not affect any other component's text field or whatever.
you can't oop-trickery yourself out of that like you wanted to.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

pgimeno
Party member
Posts: 2311
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to correctly read text from keyboard

cristoferfb wrote:
Sun May 10, 2020 12:19 am
and this is my code:

Code: Select all

[...]
function love.draw ()
functionView:draw()
end
[...]

I'm quite confused by your code, because you should not be calling methods on classes, you should be calling them on objects. Class methods don't need colon syntax; only object methods do, similarly to how in C++ static methods use :: and object methods use . or ->.

cristoferfb wrote:
Sun May 10, 2020 12:19 am
EDIT:

As the title say I want to read text from keyboard, not how to read a key, I need emulate the behaviour of a input text in Android. Then, as u can see in the example code, I read about the love.textinput event handler but the class thats need it is inside of 2 parents class, so I obviusly can break some paradigms for take it more easy, but speaking of real projects with a certain complexity that needs order and nice practices, there is a relatively clean way of do it?
What you are asking here is a GUI input widget. Löve doesn't include a GUI; you need to implement it using the basic events. Often the GUI class hierarchy is somewhat separate from the game's; see my signature for an example of a GUI library (GSpöt) that perhaps can help orienting you.

Who is online

Users browsing this forum: No registered users and 13 guests