SFML community forums
Help => General => Topic started by: MaZy 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".
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
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]
-
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
Text.SetText(i + std::string(": 1");
Use instead Event::TextEntered
He didn't say that he wants text input.
-
Use instead Event::TextEntered. ;)
-
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
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);
}
}
-
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.