Custom Keyboard Mapping

Started by TheJBurger, Sat 12/03/2011 20:30:53

Previous topic - Next topic

TheJBurger

So I'm trying to implement a way for users to remap keyboard controls in AGS. My current idea is to create a GUI with a series of key "Buttons" referential to the re-mappable keys, and then clicking on a button would pop up a single textbox in which users can input the alternative key to remap.

The problem is (at least one of more) that I'm unsure how to get the input in the textbox translated into a form that "on_key_press" can recognize. I tried using a string -> integer, but then I don't think AGS can translate a string ("A" for example) into its ASCII equivalent? (I could be wrong) Or is there a way to do translate the texboxt input -> on_key_press ("keycode == "string"") with solely with strings? Or is there another completely better way to do it altogether? :)

Thanks!
Josh

Dualnames

#1
Josh, I've coded something

http://www.adventuregamestudio.co.uk/yabb/index.php?topic=35460.0

It may not work as intended (and it's probably mediocre coded), but it may be useful as reference.

EDIT: I also realized I'm not answering your question at all.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Khris

This should do it:

Code: ags
  int keycode = TextBoxInput.Chars[0];


You can assign an int to a char var and the other way 'round.

monkey0506

It's purely a matter of preference, but I wouldn't use a textbox at all. I would simply create another GUI to pop up when the user selects to change a key with a label on it like "Press the key you would like to use...". Then simply check in on_key_press whether the GUI is shown, and intercept key presses appropriately.

TheJBurger

Quote from: Khris on Sat 12/03/2011 20:58:40
This should do it:

Code: ags
  int keycode = TextBoxInput.Chars[0];


You can assign an int to a char var and the other way 'round.

Thanks! This bit works great. How does it work to turn the ascii # back into a string matching it again?

Quote from: monkey_05_06 on Sat 12/03/2011 22:06:49
It's purely a matter of preference, but I wouldn't use a textbox at all. I would simply create another GUI to pop up when the user selects to change a key with a label on it like "Press the key you would like to use...". Then simply check in on_key_press whether the GUI is shown, and intercept key presses appropriately.
Sounds like a good idea. Is there a command to simply check "WhatKeyIsUserPressing" and grab that instead of going through them one by one?

Aside from that, I'm running into this new issue below:
Code: ags

// repeatedley execute always

// all key remaps are in a listbox

// keyboard remapping
      if (txtNewControl.Visible) { // yes
      }
      
      if (txtNewControl.Text != "") {
        int conflict=txtNewControl.Text.Chars[0]; 
        
        if (conflict != keyNew && (conflict == keyReload)) {
          Display("Error: key already in use - first re-assign in-use key");
          }
          
        else {
          
          // Update the List box
          String ctrl = txtNewControl.Text.UpperCase();
          
          // sets reload key...
          keyNew=txtNewControl.Text.Chars[0];
          // This should be "R"
          
          // This is to set the new KEY in both CODE and for the LISTBOX
          if (lstControls.Items[lstControls.SelectedIndex].Contains("reload")) {
            ctrl = ctrl.Append(": Reload"); // append w/ correct keyName
            keyReload=keyNew; // set new reload key... right!?
            Display("reload! %d", keyReload);
            // game displays "114" when AGS manual says R="82" ascii table
            
            }
          
          // Clear
          lstControls.Items[lstControls.SelectedIndex]=ctrl;
          txtNewControl.Text="";
          
          }
      
        gNewControl.Visible=false;
        
      }
    // end keyboard mapping  


I've got the bit where the textbox input translates into an ASCII code. The trouble is that the ASCII code doesn't seem to match what it should in order to remap the key.

For instance, in the AGS manual, it lists the key "R" as keycode 82. When I remap it while playing the game (I input "R") I do a check on the new code, and it lists as 114, which results in the remap not working. Other remaps for keys such as 1-10 (on the keyboard, not in the ASCII table) are remapped correctly, but for some reason the normal alphabet letters (A-Z) do not correspond to what they are in the manual.

Help!

monkey0506

#5
The reason it's returning 114 instead of 82 is that the 'r' in question is lowercase. You should do this before checking the chars:

Code: ags
txtNewControl.Text = txtNewControl.Upper();


That ensures that the keycodes will match what is in the manual. You may also want to note that in addition to using the decimal value of the ASCII characters directly, you can also use the enumerated values like eKeyR, or you can use a character literal such as 'R' (the single quotes denote a single character just like double quotes indicate a string of text).

As for what key the user is pressing, within the on_key_press function you would simply check the keycode parameter. This would actually be a lot simpler to implement this way..but I'll save a code example until I'm not on my phone. :P

Oh, and as for converting an ASCII value into a string, you would want to use String.Format:

Code: ags
String asciiRDec = String.Format("%d", eKeyR); // gives the string "82", the decimal value of key R
int intR = asciiRDec.AsInt; // gives the value of 82
String asciiR = String.Format("%c", 'R'); // here we use the character literal, a different way of doing the same thing, but use a String of "R"
char charR = asciiR.Chars[0];


Okay, so no code examples was a lie, but later. :=

SMF spam blocked by CleanTalk