Null keymodifier in type()

Asked by Alex Lunyov

Is there any way to set "null" keymodifier (let call it "Key.Null") in the type function? So that these lines were equivalent:

type("c")
type("c",)
type("c",Key.Null)

I need it to call type() with and without keymodifiers in the same line of a code. I suggest this feature (in case it was not implemented yet) could be useful for such purposes in Sikuli.

Question information

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

What is the reason? not really clear what you want.

Revision history for this message
Alex Lunyov (lunyov-av) said :
#2

Sorry, if my problem is not worth taking your attention.

I use the following workaround:

def typeCopyCommand(key="c", keym=Key.CTRL):
    if keym:
        type(key,keym)
    else:
        type(key)

In my code typeCopyCommand() sometimes need only one argument: Key.ENTER, to copy what I need. In this case I use the function typeCopyCommand() as following: typeCopyCommand(key=Key.ENTER,keym="")

I think it could be clearer to use Key.NULL modifier in type() and rewrite code like this:

def typeCopyCommand(key="c", keym=Key.CTRL):
     type(key,keym)

and then use more clear commands:
typeCopyCommand()
typeCopyCommand(key=Key.ENTER,keym=Key.NULL) # in case the type() function could use null keymodifier

Revision history for this message
Alex Lunyov (lunyov-av) said :
#3

In short Key.NULL should act as absence of any keymodifiers, or as "pressing nothing".

Revision history for this message
RaiMan (raimund-hocke) said :
#4

--- Sorry, if my problem is not worth taking your attention.
I do not understand this negative comment, hope it is a translation problem ;-)
You surely have my attention and furthermore I appreciate, that you are trying to answer other peoples questions.
I simply did not understand your problem.

--- type(key, keymodifier)
key is a string and keymodifier can be an integer value (created from a sum of KeyModifier constants)
hence
type(key)
is the same as
type(key, 0)

so here your "null-value" simply is the number 0.

--- your function typeCopyCommand
def typeCopyCommand(key="c", keym=Key.CTRL):
     type(key,keym)

... usage:
typeCopyCommand()
typeCopyCommand(Key.ENTER, 0)

... the parameter names can be omitted, when the parameters are given in the defined sequence.

Revision history for this message
Alex Lunyov (lunyov-av) said :
#5

Great, thanks! This entirely solved my problem. :)

Revision history for this message
Alex Lunyov (lunyov-av) said :
#6

Some addition. For beginners (like me) and for compatibility it could be useful to create constant Key.NULL=0, but it is just my humble suggestion. As for my strange comment, that was just my excuse for the problem insignificance, but nevertheless I'm glad to get know the solution.

Revision history for this message
RaiMan (raimund-hocke) said :
#7

--- excuse for the problem insignificance
you should never do that and never apologize for having questions.
Just try to clearly describe what you want to do/achieve and what you already have tried together with some information on outcome.

--- your suggestion
finally I have to admit, that in your situation a Key.NULL meaning "no-modifier-keys" makes some sense: it simply makes your code a bit more readable.
Feel free, to define it yourself somewhere at the beginning (startup section or some imported config script):
Key.NULL = 0

... and later use it as suggested.

For the API it does not make sense though (if at all, then the name should be Key.NONE, because Key.NULL could be misleading).

my solution for your problem would have been:

def typeCopyCommand(key=None, keym=None):
    if not key and not keym:
        type("c", Key.CTRL)
    elif keym:
        type(key,keym)
    else:
        type(key)

Revision history for this message
Alex Lunyov (lunyov-av) said :
#8

OK, I got it. Agree with your code decision, I have included it in my script. Thanks!