Issue with type()

Asked by Fabian

Hi all,

I'm facing an issue when overloading "type" method.

main.py
--------
import MyGeneral
MyGeneral.Type(Key.PAGE_DOWN)

MyGeneral.py
---------------
def Type(TextToType):
 print "MyGeneral.Type - TextToType: "+str(TextToType)
 type(TextToType)

When running main.py, either this following error is raised:
"UnicodeEncodeError: 'ascii' codec can't encode character u'\ue005' in position 0: ordinal not in range(128)"

or I caught this exception if Key.PAGE_DOWN is surrounded by "":
"MyGeneral.Type("Key.PAGE_DOWN")
 File "\\MyPath\MyGeneral.sikuli\MyGeneral.py", line 69, in Type
 type(TextToType)
at sun.awt.windows.WRobotPeer.keyPress(Native Method)
at java.awt.Robot.keyPress(Unknown Source)
at org.sikuli.script.DesktopRobot.doType(DesktopRobot.java:111)
at org.sikuli.script.DesktopRobot.typeChar(DesktopRobot.java:118)
at org.sikuli.script.Region.type(Region.java:809)
at org.sikuli.script.Region.type(Region.java:789)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Invalid key code"

Any idea from the community ?

Thanks

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Fabian
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

The problem is your print statement:

 print "MyGeneral.Type - TextToType: "+str(TextToType)

Since when using:

MyGeneral.Type(Key.PAGE_DOWN)
(which must be used this way without "" for type() to work correctly)

TextToType contains a unicode character that is used internally to translate Key.PAGE_DOWN to the final Java KeyCode.

This character cannot be printed with the normal print.

This print statement would work without error:

print ("MyGeneral.Type - TextToType: "+str(TextToType)).encode("utf8")

but the Key.XXX would be printed with the unicode characters (PAGE_DOWN e.g. a little square), which might not be whatr you expect.

Sorry, but there is no simple solution for that.

The only simple solution for text and one key would be something like that:

def Type(TextToType, theKey):
    print "MyGeneral.Type - TextToType: "+str(TextToType) +" "+theKey
    if TextToType != "":
        exec('type("' + TextToType '" + theKey +")")
    else:
        exec("type("+ theKey +")")

usage:
MyGeneral.Type("", Key.PAGE_DOWN)

or

MyGeneral.Type("some text", Key.DOWN)

Revision history for this message
Fabian (fabian-ledoeuff) said :
#2

OK thanks Rainman, it is what I have seen during my search.
I was hoping an easier way ...