Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: A cursor for typing  (Read 4251 times)

0 Members and 1 Guest are viewing this topic.

sludge

  • Newbie
  • *
  • Posts: 24
    • View Profile
A cursor for typing
« on: July 14, 2010, 06:19:21 pm »
In my program, there are several boxes that a user can click on.  When he clicks on these boxes, he should be able to input text, which will be stored and displayed on screen from within the confines of these boxes.

In just about every program that requires keyboard input, there is that blinking, black cursor to indicate where the next letter will be printed.  I need to create one of these.

I noticed the mention of a function in the tutorials that returns in the position of a letter inside a string; it further reads that this might be useful for cursor display.  Should i try to use this function to create my cursor, or is there a more appropriate strategy I should follow?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
A cursor for typing
« Reply #1 on: July 14, 2010, 06:24:21 pm »
Quote
Should i try to use this function to create my cursor, or is there a more appropriate strategy I should follow?

This function was made exactly for that purpose ;)
Laurent Gomila - SFML developer

sludge

  • Newbie
  • *
  • Posts: 24
    • View Profile
A cursor for typing
« Reply #2 on: July 14, 2010, 10:04:28 pm »
Alright. That sounds good.

My next question regards input.  I looked in the documentation for a bool function that returns if an input event is a standard letter or number.  If this is the case, I can essentially return this key and append it to a string.  Does such a function exist?

Otherwise I am going to need to do something along the lines of
if the input == 'a' or input == 'b' and so on.

Thank you

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
A cursor for typing
« Reply #3 on: July 14, 2010, 10:22:40 pm »
Quote from: "sludge"
I looked in the documentation for a bool function that returns if an input event is a standard letter or number.
Maybe you are looking for std::isalnum() in the C++ standard library.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
A cursor for typing
« Reply #4 on: July 14, 2010, 11:17:15 pm »
Do you really need to filter letters and numbers (for whatever reason), or in fact you want to filter interesting things in KeyPressed events?

If the second thing is what you wanted to do to implement text input (a lot of users do this mistake), you should rather have a look at the TextEntered event.
Laurent Gomila - SFML developer

sludge

  • Newbie
  • *
  • Posts: 24
    • View Profile
A cursor for typing
« Reply #5 on: July 15, 2010, 04:16:40 pm »
I suppose that I want to do the latter.  In essence, whenever a number is pressed, I want to add it to the text member inside an sf::String.

The only member of a sf::Event::TextEvent is a public unicode member.  Is there an example of how I can use this input?

Also, suppose I have a class with a member sf::String.  If the delete key is pressed, I want to delete the last character of the text member inside this string. I'm sure there is an easier way to do this than what I was trying to do:


Code: [Select]
std::string(text.GetText()).erase(std::string(text.GetText()).length() - 1);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
A cursor for typing
« Reply #6 on: July 15, 2010, 05:07:33 pm »
The best solution for text input is to store the text itself in its own std::string (or whatever) and update the sf::String whenever it changes. Don't directly use the sf::String's internal text, it's not made to be easily editable.

You can find plenty of examples on this forum ;)
Laurent Gomila - SFML developer