Archived:How to simulate a keypress in PySymbian

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Article Metadata
Created: Rafael T. (24 Oct 2008)
Last edited: hamishwillee (28 Jun 2012)



Some people feel the absence of a module for simulating keypresses when programming for S60 3rd Edition in Python. For doing this, you must use keypress module.




Available Functions

simulate_key(keycode, scancode [, modifiers])

Simulates a keypress. For keycode, scancode and modifiers, use constants from key_codes module.

simulate_event(event, keycode, scancode [, modifiers [, repeats]])

Simulates a single key-down, key or key-up event. The event parameter is either EEventKeyDown, EEventKey or EEventKeyUp defined in either appuifw or keypress module itself. The keycode, scancode and modifiers params are the same as for the simulate_key(). The repeats i a number of generated auto repeats (an integer).

send_raw_event(event [, param1 [, param2]])

Sends one of the raw events defined in the keypress module (check out c:\resource\keypress.py after installation). Most useful are EKeyUp and EKeyDown. Events with one or two params are supported, the key events use one param which is a scancode of the key (EStdKey*** or EScancode*** defined in key_codes).

simulate_key_mod(keycode, scancode [, modifiers])

Same as simulate_key().


Simulates right softkey press.

Example Code

import keypress
from key_codes import *
#Numeric keys
keypress.simulate_key(EKey1, EScancode1) # Simulates '1' key
keypress.simulate_key(EKey2, EScancode2) # Simulates '2' key
keypress.simulate_key(EKey3, EScancode3) # Simulates '3' key
keypress.simulate_key(EKey4, EScancode4) # Simulates '4' key
keypress.simulate_key(EKey5, EScancode5) # Simulates '5' key
keypress.simulate_key(EKey6, EScancode6) # Simulates '6' key
keypress.simulate_key(EKey7, EScancode7) # Simulates '7' key
keypress.simulate_key(EKey8, EScancode8) # Simulates '8' key
keypress.simulate_key(EKey9, EScancode9) # Simulates '9' key
#Other keys
keypress.simulate_key(EKeyStar, EScancodeStar) # Simulates Star key
keypress.simulate_key(EKeyHash, EScancodeHash) # Simulates Hash key
keypress.simulate_key(EKeyLeftArrow EScancodeLeftArrow) # Simulates Left arrow key
keypress.simulate_key(EKeyUpArrow, EScancodeUpArrow) # Simulates Up arrow key
keypress.simulate_key(EKeySelect, EScancodeSelect) # Simulates Select key
keypress.simulate_key(EKeyRightArrow, EScancodeRightArrow) # Simulates Right arrow
keypress.simulate_key(EKeyDownArrow, EScancodeDownArrow) # Simulate Down arrow
keypress.simulate_key(EKeyLeftSoftkey, EScancodeLeftSoftkey) # Simulates Left Softkey
keypress.simulate_key(EKeyMenu, EScancodeMenu) # Simulates Menu key
keypress.simulate_key(EKeyYes, EScancodeYes) # Simulates Green key
keypress.simulate_key(EKeyNo, EScancodeNo) # Simulates Red key
keypress.simulate_key(EKeyBackspace, EScancodeBackspace) # Simulates Backspace key
keypress.simulate_key(EKeyEdit, EScancodeEdit) # Simulates Edit key
#Right softkey
keypress.simulate_keyMenu() # Simulates Right Softkey

Sending a raw event

import keypress
from key_codes import *
#For raw event, always use scancodes
keypress.send_raw_event(keypress.EKeyDown, EStdKeyNo)
keypress.send_raw_event(keypress.EKeyUp, EStdKeyNo)

Simulating characters

Sometimes you do not want to simulate a key but simulate the input of a character instead. This can be easily achieved by just passing in the unicode representation of any character:

keypress.simulate_key(ord(unicode_character), 0)
#e.g. ä
keypress.simulate_key(ord(u"ä"), 0)

For numbers it is a little bit more complicated because just passing 0x32 (for the number 2) will in most cases just print 'a' (or 'b','c' depending on how often it is simulated in conjunction).

For number keys use:

keypress.simulate_key(number_in_ascii, 0, 128)
#e.g. for 2
keypress.simulate_key(0x32, 0, 128)


Keypress is a very complete module. You can simulate almost any key that your phone has. Hope this article help you to use it :)

I haven't tested all the codes, but they seem to be right. Anyway, if you find an error or some addictional code, please edit this article.

This article was written by Rafael T.

See also

Bold text

This page was last modified on 28 June 2012, at 09:26.
101 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.