Results 1 to 10 of 38
I want to make a program that will simulate several pressed keys.
Any help would be appreciated....
- 12-14-2008 #1Just 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.
- 12-14-2008 #2
Well, that was fun:
Or did you have something more specific in mind?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)--
Bill
Old age and treachery will overcome youth and skill.
- 12-14-2008 #3Just 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)
- 12-14-2008 #4Um, 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.Also, I got an error at the end saying it couldn't find the command on line 22.
The current case is a perfect example of this. I get the following error message:
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?Code:./3.sh: line 22: The: command not found
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:
try this:Code:command="The rain in Spain stays mainly in the plain."
and, um, fix a (cough) bug I made at the end of the script. Instead of this:Code:command="ls -l /usr"
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!Code:$command
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 meant to ask how do I make a program that will simulate keystrokes, like <enter> or <tab> and all the other keys.
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.
- 12-14-2008 #5Just Joined!
- Join Date
- May 2008
- Posts
- 30
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.
- 12-14-2008 #6Just 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).
- 12-14-2008 #7Just Joined!
- Join Date
- May 2008
- Posts
- 30
- 12-15-2008 #8
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.
- 12-19-2008 #9
fiomba, have you found anything with xmacro yet?
--
Bill
Old age and treachery will overcome youth and skill.
- 12-19-2008 #10Just 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.
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.Code:xsendkeycode # #
So,
sends 'a' to the X server.Code:xsendkeycode 38 1 xsendkeycode 38 0
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.


Reply With Quote
