Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2013
    Rep Power

    Default how can i create a string that show what key the user pressed?

    so im realy new to java, and i only know the basics like int and Strings n constructors(just a little bit) and stuff like how to user other classes and yeh,
    and then i watched some tutorials on how to create a game using JFrame, and now i am working on a basic game, just like mario and i wanted to show what the user pressed if the user pressed "f1" first.
    so what i did was i made a paint method and i did like this: "g.drawString(keyPressed, keyPressedX, keyPressedY);" and then i created the "keyPressedX" & "keyPressedY" and i made them -1000 by default.
    and then i went and created (watched alot of tutorials untill i figured it out xD ) a class that checked for userInput and i said "public void keyPressed(KeyEvent e){ int keyCode = e.getKeyCode();}" and then i did this(in the method):
    "if(keyCode == e.VK_f1){ keyPressedX = 10; keyPressedY = 40;}"
    and well it worked, i got the text to appear when i pressed "f1", but now i have to do the harder stuff witch is making the text be something like this: ("keyPressed: " + keyCode);
    (keyPressed + whatever i pressed)
    but i have no idea how to do that, i'v tried to create an String called userPressed that was equals to (""); (nothing) by default and then i went to the method that changed the x and y when i press "f1" and i said: userPressed = ("keyPressed: " + keyCode); and as i said, i have no idea how i can make this so i just tried stuff like: ("keyPressed: " + keyCode.getKeyCode()); and just random stuff like that, but ofc non of the stuff i tried worked

    -so is there anyone who can tell me how i can make it so that when u press "f1" the user will see a text saying like "keyPressed: (key)" ????

    thanks :D

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: how can i create a string that show what key the user pressed?

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Rep Power

    Default Re: how can i create a string that show what key the user pressed?

    Deffinetly take a look at these, but just a word of advice: If you don't already know Java Swing (GUI), then don't bother looking at the KeyListener tutorials yet.

Similar Threads

  1. Login in java show all user and all usertype
    By Fuhans in forum Advanced Java
    Replies: 1
    Last Post: 11-18-2013, 12:17 PM
  2. I want to Show user name on pages.Plz rply
    By amar_dixit4u in forum New To Java
    Replies: 0
    Last Post: 04-29-2013, 10:54 AM
  3. Show Capcha to user
    By omercoh in forum Networking
    Replies: 0
    Last Post: 01-27-2011, 06:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts