Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for SendKeys module #183

Closed
sasumner opened this issue Jan 12, 2021 · 9 comments
Closed

Support for SendKeys module #183

sasumner opened this issue Jan 12, 2021 · 9 comments
Labels
Milestone

Comments

@sasumner
Copy link

I've notice that in 1.5.4 there seems to be partial support for the SendKeys module import:

>>> import SendKeys
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\...\plugins\PythonScript\lib\SendKeys.py", line 16, in <module>
    from _sendkeys import char2keycode, key_up, key_down, toggle_numlock
ImportError: No module named _sendkeys

So, SendKeys is found, but when that code tries to access _sendkeys there is a failure because that isn't found.

I'd like to use SendKeys without having to do "special things" to make it work.

Note about 3.0.5alpha: SendKeys is not present at all -- can this be added (in full, of course)?

@chcg chcg added the Bug label Jan 15, 2021
@chcg chcg added this to the v2.0 milestone Jan 15, 2021
@chcg
Copy link
Collaborator

chcg commented Jan 15, 2021

See https://pypi.org/project/SendKeys/#files and https://www.reddit.com/r/learnpython/comments/7lzocc/sendkeys_not_working_python_3/, it seems it is just available for python 2.7

The download contains in setup.py

    ext_modules=[
        Extension("_sendkeys", ["_sendkeys.c"],
            libraries=['user32','kernel32']),
    ],

, so indeed there seems to be some part missing in the current installation for python 2.7

Just found:
https://github.com/bruderstein/PythonScript/blob/stable_python27/PythonLib/full_dll/_sendkeys.pyd
, but not the counterpart for x64. Additionally it doesn't make sense to include sendkeys.py for the minimal build under https://github.com/bruderstein/PythonScript/blob/stable_python27/PythonLib/min/SendKeys.py if _sendkeys.pyd is not available in this configuration.

Maybe https://code.google.com/archive/p/sendkeys-ctypes/downloads is a python only replacement which works for x86, x64 and also for the minimal package.

@sasumner Did you use the x64 bit version of 1.5.4?

@sasumner
Copy link
Author

@sasumner Did you use the x64 bit version of 1.5.4?

No; was using 32-bit.

@chcg
Copy link
Collaborator

chcg commented Jan 15, 2021

@sasumner Just tried it with 32bit and it worked fine for me. Is _sendkeys.pyd available in your N++ plugins\PythonScript\lib dir?

Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:19:08) [MSC v.1500 32 bit (Intel)]
Initialisation took 63ms
Ready.
>>> import SendKeys

Have you changed the configuration which python installation is preferred?

@sasumner
Copy link
Author

No; was using 32-bit.

I'm actually using both 32 and 64-bit, so sometimes it gets confusing. :-)
Sorry for that.

Clarification:

  • with 32-bit: no errors with import SendKeys
  • with 64-bit: import SendKeys yields the ImportError: No module named _sendkeys error this issue was opened with.

Is _sendkeys.pyd available in your N++ plugins\PythonScript\lib dir?

  • with 32-bit: YES
  • with 64-bit: NO

Have you changed the configuration which python installation is preferred?

No; I have no independent Python 2.7 on my machine.

@chcg
Copy link
Collaborator

chcg commented Jan 15, 2021

@sasumner That makes much more sense and is matching with my findings from the code :-)
Could you test if adding _sendkeys.py from https://code.google.com/archive/p/sendkeys-ctypes/downloads to your x64 version is working fine for you. And do you have a testscript using Sendkeys? Maybe I could make a unittest from it to check it automatically in the future.

chcg added a commit that referenced this issue Jan 15, 2021
@sasumner
Copy link
Author

@chcg In the future, having SendKeys available with PS 3.x will be important to me, as part of a "full" install of PythonScript. I see that the import fails under the current 3.0.5alpha (64 bit). Will SendKeys be something that will be available in PS 3 (real release, non-alpha)?

chcg added a commit that referenced this issue Jan 16, 2021
added python3 adapted version
@chcg chcg modified the milestones: v2.0, v3.0 Jan 16, 2021
@chcg
Copy link
Collaborator

chcg commented Jan 16, 2021

@sasumner Added a python3 adapted version of SendKeys also to master for 3.0.x version.

@sasumner
Copy link
Author

@chcg said:

And do you have a testscript using Sendkeys? Maybe I could make a unittest from it to check it automatically in the future.

I use SendKeys for two main reasons.

1. autodismiss for notepad.messageBox()

Example:

# -*- coding: utf-8 -*-

from Npp import notepad
import threading
import SendKeys as sk

def msg_box_auto_dismiss(message, title='', flags_to_add_to_ok=None, secs_to_dismiss_after=3.0):
    # don't try to do a multiple button message box with this!
    user_dismissed = False
    if secs_to_dismiss_after > 0:
        def sendEnter():
            if not user_dismissed:
                sk.SendKeys("{ENTER}")
        timer = threading.Timer(secs_to_dismiss_after, sendEnter)
    flags = MESSAGEBOXFLAGS.OK
    if flags_to_add_to_ok != None: flags |= flags_to_add_to_ok   # can add .ICON* items (e.g. MESSAGEBOXFLAGS.ICONINFORMATION)
    if secs_to_dismiss_after > 0:
        timer.start()
        message += '\r\n' * 6 + 'This box will auto-close in {} seconds'.format(secs_to_dismiss_after)
    retval = notepad.messageBox(message, title, flags)
    user_dismissed = True
    return retval

msg_box_auto_dismiss("Press OK ... or don't")

2. I use the notepad.prompt() box as a UI sometimes. (It is not a good general-purpose UI, but resorting to something like Tk is overkill.)

Example:

notepad.prompt('Enter your info:\r\n(put an "x" inside the [  ] to select)', '', 'first name:\r\nlast name:\r\n[  ]blue-eyes  [  ]green-eyes  [  ]brown-eyes  [  ]other')

which produces:

image

However, that is annoying because the "prompt" call leaves all text selected. If I do this instead:

threading.Timer(0.25, lambda : sk.SendKeys('{END}')).start()
notepad.prompt('Enter your info:\r\n(put an "x" inside the [  ] to select)', '', 'first name:\r\nlast name:\r\n[  ]blue-eyes  [  ]green-eyes  [  ]brown-eyes  [  ]other')

then I obtain:

image

which is better, but still not great because the user input caret will be left after the r in "other" (as shown). So I adjust it still further to be sk.SendKeys('{UP}{UP}{END}'), which finally gives me what is best (the user input caret after the : in the first name: text:

image

Note: This "SendKeys" adaptation would be eliminated if there was a notepad.messageBoxEx() function which would allow me to (a) cancel the default-text being auto-selected and (b) specify the desired caret position. Perhaps I should have requested that as a feature, instead of my SendKeys complaint. :-)

@chcg
Copy link
Collaborator

chcg commented Feb 18, 2022

Fixed with https://github.com/bruderstein/PythonScript/releases/tag/v2.0.0 and also in the python3 alpha versions.

@sasumner Feel free to add a new request for the messagebox.

@chcg chcg closed this as completed Feb 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants