Hello,
I made a GUI that looks like a keyboard where you have to enter a code.
Each letter of the keyboard is a button
And there is also a TextBox
So the player can enter the code using the GUI keyboard
I found the Key code table here:
https://adventuregamestudio.github.io/ags-manual/Keycodes.html?highlight=press%20key&case_sensitive=0 (https://adventuregamestudio.github.io/ags-manual/Keycodes.html?highlight=press%20key&case_sensitive=0)
But i can"t find the correct syntax i have to put for each button
For letter A i tried
function letter_A_OnClick(GUIControl *control, MouseButton button)
{
(keycode == eKeyA);
}
and a few other but this is not the correct syntax
Anyone can help ? ;-D
Thanks a lot
You're confusing this with the on_key_press function; if the player types the letter A during the game, AGS calls on_key_press(eKeyA); which you can then react to by providing the function.
This concept can be quite hard to wrap your head around at first because instead of calling a function provided by AGS, you are providing the function which is then called by the engine.
In the above example you need a way to access the eKeyA argument, which is why you put eKeyCode keycode in the parens when you write the function.
Anyway, this is how the function is supposed to look like:
function letter_A_OnClick(GUIControl *control, MouseButton button)
{
myTextBox.Text = String.Format("%sa", myTextBox.Text); // append a lowercase "a"
}
Obviously you need to use your own textbox's script name instead of myTextBox.
Note however that it's extremely tedious but also unnecessary to write 26 functions IF you make sure the button IDs are in alphabetical order.
That's because you can use a single function to handle multiple events.
What I would do is use button #0 for the A, button #1 for the B, etc.
Then you can do
function letter_OnClick(GUIControl *control, MouseButton button)
{
myTextBox.Text = String.Format("%s%c", myTextBox.Text, control.ID + 97);
}
Now you paste letter_OnClick into each letter button's event table.
Hello Khris,
Once again you give me a true masterclass of how it works.
I Was still trying to find a solution and beginning to think that I was not going the right way and this was the case.
You explain it very"instead of calling a function provided by AGS, you are providing the function which is then called by the engine." well with this sentence:
I had to read it like 3 times :) to understand but now I get it.
For the second part it's pure genius time saving and it works like a charm.
Thank you so much Krhris and i have just one more question please what is:
control.ID + 97 ? 8-)
Quote from: actaria on Tue 01/11/2022 20:50:51Thank you so much Krhris and i have just one more question please what is:
control.ID + 97 ? 8-)
97 is a code for small 'a' character. So if your control IDs start with zero, this math will make resulting printed character start with 97 or 'a'.
Please note that you can also use actual "character literal" to make the code bit more clear:
myTextBox.Text = String.Format("%s%c", myTextBox.Text, control.ID + 'a');
Also there's no need for Format there
myTextBox.Text = myTextBox.Text.AppendChar('a' + control.ID)
Thank you very much for taking the time to explain eevything so well each time.
Now i can even chosse between 3 differents syntax :)
Hello,
It's me again, everything is working well except the fact that i can't manage to make work keys:
Delete
Enter
space
I tried using control.ID + code with a lot of differents codes but none seems related to these keys ?
I tried from 0 to 300 i think :)
I could also tried this method
myTextBox.Text = String.Format("%sa", myTextBox.Text); // append a lowercase "a"
but i don't know what the syntax will be for: Delete, Enter and Space
Thanks a lot for your help
Again, the keycodes are not relevant if we're talking about an onscreen keyboard. The code provided is to add characters to the string. You cannot use this approach to delete characters from the string or do other things; you will need to code that separately.
For Backspace, you can use:
if(myTextBox.Text.Length>0) myTextBox.Text = myTextBox.Text.Trim(myTextBox.Text.Length-1);
For Delete and Enter... well, what do you want them to do? Delete usually deletes the next character after the cursor, but AGS TextBoxes don't allow you to move the cursor back, it's always at the end, so there will never be anything to delete. And as for Enter, you can't have multiple lines in a TextBox. Probably you will want it to do something like confirm or submit, but you will have to code what that means.
A Textbox has an OnActivate event that runs when the Enter key is typed. You'll want to also have this function run when the enter button is clicked, so what you need is this (assuming your textbox's script name is txtCode):
// this is the standard handler for the textbox
function txtCode_OnActivate(GUIControl* control) {
String code = txtCode.Text;
// do something with code here
}
// use this to handle the enter button (needs to be somewhere below the first function!)
function btnEnter_OnClick(GUIControl* control, MouseButton button) {
if (button != eMouseLeft) return; // ignore right / middle button
txtCode_OnActivate(txtCode); // call textbox handler
}
This should work I guess; if not you can also try txtCode_OnActivate(gKeyboard.Controls[txtCode.ID]);
Hello Snarky and thank you for you help
I found an easy solution for the space key
champ_mdp.Text = String.Format("%s ", champ_mdp.Text);
Sorry i made a mistake on my previous post keys are
Space
Delete
Return
for a virtual keyboard on a GUI
(https://zupimages.net/up/22/44/69cj.jpg)
So now i have all letters and space working.
I need the Delete and Enter keys to work so the player can validate or edit the code he will enter without using his physical keyboard only playing using the mouse.
Thank you
Quote from: Khris on Wed 02/11/2022 10:14:22A Textbox has an OnActivate event that runs when the Enter key is typed. You'll want to also have this function run when the enter button is clicked, so what you need is this (assuming your textbox's script name is txtCode):
// this is the standard handler for the textbox
function txtCode_OnActivate(GUIControl* control) {
String code = txtCode.Text;
// do something with code here
}
// use this to handle the enter button (needs to be somewhere below the first function!)
function btnEnter_OnClick(GUIControl* control, MouseButton button) {
if (button != eMouseLeft) return; // ignore right / middle button
txtCode_OnActivate(txtCode); // call textbox handler
}
This should work I guess; if not you can also try txtCode_OnActivate(gKeyboard.Controls[txtCode.ID]);
Hello Khris yes this part on Textbox OnActivate with the code is vorking well
Now i need to make the delete and and enter keys on the GUI to work when i click on them :) so the player don't have to use his physical keyboard and can do eveything with the mouse
(https://zupimages.net/up/22/44/6ict.jpg)
Thanks again for you help
You can do this for the space bar button:
myTextBox.Text = myTextBox.Text.AppendChar(' ');
Also check my previous post again, I already addressed implementing the enter key :)
As for "delete", use the backspace code Snarky suggested.
(Delete is the name for the other key, the one that deletes the character to the right of the cursor, which doesn't apply here anyway)
The key you're calling "Delete" is actually Backspace (although Apple mislabels it as "delete"), and the code I provided should work for your purpose.
O yes sorry i missed the Return key part so now it's almost perfect :)
"The key you're calling "Delete" is actually Backspace (although Apple mislabels it as "delete" yes my bad !
I tried your solution ( Snarky) for the Backspace (delete) key
if(myTextBox.Text.Length>0) myTextBox.Text = myTextBox.Text.Trim(myTextBox.Text.Length-1);
but i have an error:
GlobalScript.asc(1454): Error (line 1454): '.Trim' is not a public member of 'String'. Are you sure you spelt it correctly (remember, capital letters are important)?
Thank you
The correct function is "Truncate".
https://adventuregamestudio.github.io/ags-manual/String.html#stringtruncate
AFAIK "Trim" usually means removing whitespaces from the string's beginning and end, but AGS does not have it.
Fantastic it's working :)
Thanks to all of you I clearly coundl't have make this work without your help.
Now i have a nice working GUI Keyboard i will now do the esthetic part :)
Again thanks a lot and I wish you all a great day
Quote from: Crimson Wizard on Wed 02/11/2022 11:04:48The correct function is "Truncate".
Damn, I even looked it up, and still managed to type the wrong thing. :-\