0

I am creating a text adventure game in eclipse. I would like to know how while you are in the console you can press a key such as '1' or '2' and the computer registers this as 'attack' or 'block' being typed out. So basically what i'm asking here is: how can you press a key and instead of that one character, a whole word is entered. So when I press '1', i'm really just saying to the computer that I would like to attack. That way I wouldn't have to type out 'attack'. I can just press 1 and 'attack' is typed out for me. This would prevent people from typing out the wrong word. That's really it. People don't really seem to understand me whenever I ask questions like this so if you need me to elaborate, just say so. Thanks for any help :)

And one more thing. I don't know how this works, but please don't say that there's another question similar to this one. I think there actually is. I looked at that question though, and wow. Is it confusing as f*ck, excuse my language.

Also, btw, I don't know but i'm going into 12th grade and I believe my computer programming teacher is teaching us java. I think that's what i'm using. Again, not sure, but I think it's java.

No one knows? I feel like my programming teacher wouldn't know. No one knows anything about how to do this?

  • Eclipse is your development environment, **not** your programming language. Which of the many programming languages supported by Eclipse are you using? – greg-449 Sep 02 '18 at 19:08
  • I'm not sure but I believe i'm writing my code with java. – The Darkest Soul Sep 02 '18 at 19:14
  • Nobody know? Maybe just something simply like char "1" = "attack"; or something similar. No one knows? You people are supposed to be beyond smart,,, – The Darkest Soul Sep 03 '18 at 02:56
  • [Problem hint](https://stackoverflow.com/q/1066318). [Solution hint](https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html). Coding adventure: find out yourself. ;-) – howlger Sep 03 '18 at 09:30
  • Can I get some help here? I really did look into those but they are quite confusing. I'm not calling myself dumb, but I'm not Gabe Newell here. – The Darkest Soul Sep 03 '18 at 15:13
  • I thought in at the top of a while loop I would have: final int KEY_RIGHT = (0405); if (0405 =! false){ System.out.println("attack"); return; break; userinput = input.nextLine(); } But that didn't work – The Darkest Soul Sep 03 '18 at 15:15
  • The keystroke happens outside of your Java application. When _Enter_ is hit, the whole string is passed to your Java application. A workaround would be to open a separate window with a text field (e. g. with Java Swing) and catch keystrokes with a [key listener](https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html). But if you are a novice Java programmer, it would be a good idea to start with something easier and the player has to press Enter. – howlger Sep 03 '18 at 20:24
  • I can look into the key listener but would people be able to just run my game on a different computer without java swing open and still be able to do this? That's kinda what I want. I don't even think you can do it but I want people to be able to press a key like '1' or 'w' or something and the computer registers this as them wanting to attack or block. If I can do this without the player having to download anything but maybe eclipse that would be ideal. Also, does anyone know if you can export a file from eclipse and open it on a computer that doesn't have eclipse installed? Thanks :) – The Darkest Soul Sep 03 '18 at 23:09
  • On every computer, on which [Java is installed](https://java.com/inc/BrowserRedirect1.jsp), a Java application can be run. The Java Swing UI toolkit is included in Java; there is no additional installation required. I would recommend you to take some time to learn Java. This really takes time and it is frustrating to set goals that cannot be reached (I know this from my own experience). – howlger Sep 04 '18 at 12:14

0 Answers0