Skip to content Skip to sidebar Skip to footer

Pexpect Send Cursor Movement

How can you send cursor movements like the up,down,left,right keys with pexpect. The example below is to automate elinks which uses the up/down keys to select different links on a

Solution 1:

The below script has the codes for all the four cursor movements, with an example of how one might use it in pexpect. To discover the exact string sequences for any typed in text you may use the get_keys.py script below.

KEY_UP = '\x1b[A'
KEY_DOWN = '\x1b[B'
KEY_RIGHT = '\x1b[C'
KEY_LEFT = '\x1b[D'
child.sendline(KEY_DOWN * 5)  #send five key downs

get_keys.py

importcursesscreen= curses.initscr()

screen.addstr("Press any set of keys then press enter\n") 
keys = ''while True:
   event = screen.getkey()
   ifevent== "\n":
       break
   keys += event

curses.endwin()
print repr(keys)

Solution 2:

How about using escape sequence for up(^[[A) or down(^[[B) like this.

child.send("\033[A")  # up
child.send("\033[B")  # down

Solution 3:

try this send '\033\117\102' for down key

Post a Comment for "Pexpect Send Cursor Movement"