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

Author Topic: Any Key and Settext  (Read 5248 times)

0 Members and 1 Guest are viewing this topic.

MaZy

  • Newbie
  • *
  • Posts: 7
    • View Profile
Any Key and Settext
« on: December 16, 2009, 07:32:01 pm »
Hi, I am new on sfml and ever in programming(game, just small things).
I have tested small things like play music and load images. I've tested now to press "a" it shows then "A: 1" (like its true) and if I don't press a then "A: 0".


Code: [Select]

if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::A))
{
Text.SetText("A: 1");
}
else if ((Event.Type == sf::Event::KeyReleased) && (Event.Key.Code == sf::Key::A))
{
Text.SetText("A: 0");
}

No i got the idea to make for all Keys from A to Z.
I know i need for() for this one, but i don't really know how.

I know it should be then
sf::Key::i and Text.SetText(i + ": 0");
But my problem is I don't know how to count keys of sfml from event. If i put my mouse on A it shows me =97 and Z = 122.
so was my idea to make
Code: [Select]

for(int i=sf::Key::A; i <= sf::Key::B; i++)
{
char Buffer[5];
//sprintf(Buffer,"%d\n", i);
//MessageBox(NULL,Buffer,"Error",MB_OK | MB_ICONEXCLAMATION | MB_TOPMOST);

if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == i))
{
Text.SetText(i+": 1");
}
else if ((Event.Type == sf::Event::KeyReleased) && (Event.Key.Code == i))
{
Text.SetText(i+": 0");
}

Its working but my problem is now in SetText. I don't know how to show A or B.[/code]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Any Key and Settext
« Reply #1 on: December 16, 2009, 07:37:58 pm »
You can iterate between Key::A and Key::Z, but in rare cases it may not work. Letter key codes in SFML are mapped to their character value, which may not be ASCII. In EBCDIC for example, letters from A to Z are not in a continuous range of codes. But anyway, I don't know any environment not working with ASCII, so you can probably just ignore this stuff :)

For your second problem, you can do this
Code: [Select]
Text.SetText(i + std::string(": 1");

Quote
Use instead Event::TextEntered

He didn't say that he wants text input.
Laurent Gomila - SFML developer

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Any Key and Settext
« Reply #2 on: December 16, 2009, 07:37:55 pm »
Use instead Event::TextEntered. ;)
SFML / OS X developer

MaZy

  • Newbie
  • *
  • Posts: 7
    • View Profile
Any Key and Settext
« Reply #3 on: December 16, 2009, 07:57:49 pm »
But it works.
For test i've choosen A and B. 97 <= 98.
When i pressed the both key it changes to 1. Other key not. Its okay. How i want.

And now i want to show that 97 is A or 98 is B. I thought there is maybe something like "GetChar(98) then shows B.
So i could use SetText( GetChar(i)+: 1); // Result: A: 1

EDIT:
How is it in game if you want change hotkeys?

Omg how easy and stupid^^

Finally got it work
Code: [Select]

for(int i=sf::Key::A; i <= sf::Key::Z; i++)
{

if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == i))
{
int tmp = i;
tmp = tmp - 32;
char Buffer[10];
sprintf(Buffer,"%c: 1", tmp);
//MessageBox(NULL,Buffer,"Error",MB_OK | MB_ICONEXCLAMATION | MB_TOPMOST);
Text.SetText(Buffer);
}
else if ((Event.Type == sf::Event::KeyReleased) && (Event.Key.Code == i))
{
int tmp = i;
tmp = tmp - 32;
char Buffer[10];
sprintf(Buffer,"%c: 0", tmp);
Text.SetText(Buffer);
}
}

buggy123

  • Newbie
  • *
  • Posts: 26
    • View Profile
Any Key and Settext
« Reply #4 on: December 17, 2009, 06:00:29 am »
I believe hotkeys are done by reading stored text files. (scripting)
This way the program can read/write the the text file to access/change the hotkeys.