Find the answer to your Linux question:
Page 1 of 4 1 2 3 4 LastLast
Results 1 to 10 of 38
I want to make a program that will simulate several pressed keys. Any help would be appreciated....
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    30

    Simulating pressed keys in shell script

    I want to make a program that will simulate several pressed keys.

    Any help would be appreciated.

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Well, that was fun:
    Code:
    #!/bin/bash
    
    command="The rain in Spain stays mainly in the plain."
    
    cmdlen=${#command}
    
    echo -n ">> "
    
    position=0
    
    while [[ $position -lt $cmdlen ]]
    do
      echo -n "${command:$position:1}"
    
      sleep 0.3
    
      position=$(($position+1))
    done
    
    echo
    
    $($command)
    Or did you have something more specific in mind?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    May 2008
    Posts
    30
    I meant to ask how do I make a program that will simulate keystrokes, like <enter> or <tab> and all the other keys.

    But that was an entertaining program.

    Also, I got an error at the end saying it couldn't find the command on line 22.

    Line 22:
    Code:
    $($command)

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Also, I got an error at the end saying it couldn't find the command on line 22.
    Um, when reporting an error, it's always far better if, rather than paraphrasing the error message, you quote the exact error message (using copy and paste into your browser). The clue can often be found in the details which are lost in your paraphrasis.

    The current case is a perfect example of this. I get the following error message:
    Code:
    ./3.sh: line 22: The: command not found
    Hmm. The word "the" doesn't usually appear at all in geekish error messages. But it's here. Further, it's followed by a colon, and its first letter is capitalized. Very strange. What's going on here?

    Kinda makes you want to focus on the word "The", doesn't it? Look back in the script. There's a "The" there, capitalized even. bash is complaining because the command "The" could not be found.

    In the script, instead of this:
    Code:
    command="The rain in Spain stays mainly in the plain."
    try this:
    Code:
    command="ls -l /usr"
    and, um, fix a (cough) bug I made at the end of the script. Instead of this:
    Code:
    $($command)
    I should have said this:
    Code:
    $command
    The first "error" was deliberate. The second one was my boo-boo. Make both changes and see how it runs!

    Now, on to business:
    I meant to ask how do I make a program that will simulate keystrokes, like <enter> or <tab> and all the other keys.
    Well, this program does that! It makes it look like you're typing in the characters, and then it executes the command as though you had typed in the characters.

    I'm guessing, though, that this is not what you want. (Actually, I knew that before I wrote the script, which was part of the fun!)

    Now. How does what you want differ from this? Your request, so far, is more vague than it sounds, because you know exactly what you want, but we're on the outside looking in.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just Joined!
    Join Date
    May 2008
    Posts
    30
    Quote Originally Posted by wje_lf View Post
    Um, when reporting an error, it's always far better if, rather than paraphrasing the error message, you quote the exact error message (using copy and paste into your browser). The clue can often be found in the details which are lost in your paraphrasis.

    The current case is a perfect example of this. I get the following error message:
    Code:
    ./3.sh: line 22: The: command not found
    Hmm. The word "the" doesn't usually appear at all in geekish error messages. But it's here. Further, it's followed by a colon, and its first letter is capitalized. Very strange. What's going on here?

    Kinda makes you want to focus on the word "The", doesn't it? Look back in the script. There's a "The" there, capitalized even. bash is complaining because the command "The" could not be found.

    In the script, instead of this:
    Code:
    command="The rain in Spain stays mainly in the plain."
    try this:
    Code:
    command="ls -l /usr"
    and, um, fix a (cough) bug I made at the end of the script. Instead of this:
    Code:
    $($command)
    I should have said this:
    Code:
    $command
    The first "error" was deliberate. The second one was my boo-boo. Make both changes and see how it runs!

    Now, on to business:

    Well, this program does that! It makes it look like you're typing in the characters, and then it executes the command as though you had typed in the characters.

    I'm guessing, though, that this is not what you want. (Actually, I knew that before I wrote the script, which was part of the fun!)

    Now. How does what you want differ from this? Your request, so far, is more vague than it sounds, because you know exactly what you want, but we're on the outside looking in.
    Okay, well, I'll give you a scenario then:

    I'm trying to get it so that when I run a program, my program will open a web browser to a certain web page and enter text into the fields without me having to input any information myself. So, say it were coming to log on to this forum. It would navigate to the site, then enter my username and password into the respective text fields.

    Does that clear things up a little?

    EDIT:

    Oh, and I'm kinda new to shell scripting, and I'd like to know how to do it, not just have it handed to me. So could you explain to me what's happening in your scripts? Thanks.

  6. #6
    Just Joined!
    Join Date
    Oct 2004
    Posts
    62
    Hi all,
    I think that Cogitati is referring to "macro programming" that is to run from the shell
    an X application and to command it by means of a script that replays a sequence
    of simulated keys.
    I am (after a long time) near to a solution. I must test xmacro and if the tests succeed
    I will let you know.
    However I must warn you: the purists don't absolutely agree with this approach
    (for security reasons).

  7. #7
    Just Joined!
    Join Date
    May 2008
    Posts
    30
    Quote Originally Posted by fiomba View Post
    Hi all,
    I think that Cogitati is referring to "macro programming" that is to run from the shell
    an X application and to command it by means of a script that replays a sequence
    of simulated keys.
    I am (after a long time) near to a solution. I must test xmacro and if the tests succeed
    I will let you know.
    However I must warn you: the purists don't absolutely agree with this approach
    (for security reasons).
    Who are "the purists"?

    And yes, that sounds dead-on, except for the replayed part. I just want it to do it once per script-run.
    Last edited by Cogitati; 12-15-2008 at 08:26 PM. Reason: typo: relayed -> replayed

  8. #8
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    It looks like fiomba has a handle on what you need, Cogitati, and (given the additional details you give) it's not clear I'd be able to help. So to keep from muddying up the thread, I'll step back and let you folks talk, and I'll jump back in when it looks like you're finished (unless I can't help myself).
    --
    Bill

    Old age and treachery will overcome youth and skill.

  9. #9
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    fiomba, have you found anything with xmacro yet?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  10. #10
    Just Joined!
    Join Date
    May 2008
    Posts
    30
    Well, I found out how to do do it the manual way. I know xmacro is a lot easier, but you could make the script yourself.

    Code:
    xsendkeycode # #
    The first number is the keycode of the key you want. The second number is where it is pressed or released; 1 = pressed, 0 = released.

    So,

    Code:
    xsendkeycode 38 1
    xsendkeycode 38 0
    sends 'a' to the X server.

    If I left out the second line, it would continue to send a to the X server until it got the command to stop.

    You can find out what the keycodes are by typing "xev" in the terminal and typing the key. It also tells you mouse movements and focus loss/gain.

    When you run this program, it sends your keystrokes to the X server, so if you have the focus on the username field of your email, then it will type whatever you told it to type there.

Page 1 of 4 1 2 3 4 LastLast

Posting Permissions

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