Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Random key press

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    13
    My Mood
    Relaxed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Random key press

    Hello again!

    Now I need to know how to simulate random key stroke. Not all keys - only 'A' and 'D'.
    I don't know how to do that. Maybe

    simulate random float number from 0 to 20. If x=0-10 then press 'A' Else press 'D' ?

    Can it be like that? Can you help me or give me some basic code to generate random float number?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Random key press

    Take a look at the Math.random() method as well as the Random class. The API is your friend: Java Platform SE 6
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Random key press

    What do you mean by "simulate key stroke"?

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    13
    My Mood
    Relaxed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Random key press

    Quote Originally Posted by Norm View Post
    What do you mean by "simulate key stroke"?
    to write a loop that "press" (simulate pressing) a key on keyboard.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Random key press

    What will be the result of the "press"?
    Do you want to send a KeyEvent object to a listener?
    Or what?

    The Robot class can send key presses to a program.

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    13
    My Mood
    Relaxed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Random key press

    I know how to write robot to press a key. I need to know how to write event that choose random keys (key 'A' or key 'D').

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Random key press

    What will be the result of the "press"?
    Do you want to send a KeyEvent object to a listener?
    Look at the KeyEvent class, you can create an instance of that by using its constructor.

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Replies: 5
    Last Post: July 7th, 2011, 09:22 AM
  3. How to press button to open another window
    By vkokaram in forum AWT / Java Swing
    Replies: 1
    Last Post: July 18th, 2010, 03:51 PM
  4. press a button, get textfield content
    By chopficaro in forum AWT / Java Swing
    Replies: 1
    Last Post: May 2nd, 2010, 12:28 PM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM