Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    PythonScript Toggleable Script?

    Plugin Development
    3
    34
    572
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • John Doe 1
      John Doe 1 @Alan Kilborn last edited by John Doe 1

      @alan-kilborn Edit

      1 Reply Last reply Reply Quote 0
      • John Doe 1
        John Doe 1 @Alan Kilborn last edited by

        @alan-kilborn @alan-kilborn Sorry input was being weird had to edit this reply. You make some good points, yes the holding of the “Alt” key does present some issues, most of them minor and not a problem for my uses. Except for inserting the “t” character and it being registed as “Alt” + “t” which would switch windows (depending on your configurations), that is a bit more of an issue. Do you have any ideas of an alternate way to achieve this? Thanks for your help so far!

        PeterJones Alan Kilborn 2 Replies Last reply Reply Quote 0
        • PeterJones
          PeterJones @John Doe 1 last edited by

          @john-doe-1 ,

          If your goal is to have the “alt” key behave as “sticky”, Windows OS has an accessibility feature (“ease of access - keyboard”) for that.

          https://www.google.com/search?q=windows+10+sticky+alt+key =>
          https://winaero.com/turn-on-or-off-sticky-keys-in-windows-10/ or https://www.howtogeek.com/739764/how-to-turn-off-sticky-keys-on-windows-10/ or …

          No need to highjack the Alt key or any such thing.

          John Doe 1 1 Reply Last reply Reply Quote 1
          • John Doe 1
            John Doe 1 @PeterJones last edited by John Doe 1

            @peterjones Thanks but I wanted to make it a toolbar icon attached to a PythonScript, I realize the same function can be achieved with Keyboard Shortcuts and Sticky Keys but having a toolbar icon is just my personal preference.

            1 Reply Last reply Reply Quote 0
            • Alan Kilborn
              Alan Kilborn @John Doe 1 last edited by

              @john-doe-1 said in PythonScript Toggleable Script?:

              Do you have any ideas of an alternate way to achieve this?

              Yes, here’s a demo script I call RectangularSelectModeToggle.py. Each time it is run (hint: you can tie the running of it to a toolbar button) it toggles the shift+arrows selection functionality between selecting normal (what’s called a stream selection) and a selecting a column block (called rectangular selection). The “Alt” key is not involved. The script is fairly “low level”, and not a short one.

              # -*- coding: utf-8 -*-
              from __future__ import print_function
              
              from Npp import *
              import inspect
              import os
              import ctypes
              from ctypes import wintypes
              import platform
              
              #-------------------------------------------------------------------------------
              
              user32 = ctypes.WinDLL('user32')
              
              notepad.hwnd = user32.FindWindowW(u'Notepad++', None)
              editor1.hwnd = user32.FindWindowExW(notepad.hwnd, None, u'Scintilla', None)
              editor2.hwnd = user32.FindWindowExW(notepad.hwnd, editor1.hwnd, u'Scintilla', None)
              
              LRESULT = wintypes.LPARAM
              
              WndProcType = ctypes.WINFUNCTYPE(
                  LRESULT,  # return type
                  wintypes.HWND, wintypes.UINT, wintypes.WPARAM, wintypes.LPARAM  # arguments
                  )
              
              running_32bit = platform.architecture()[0] == '32bit'
              
              SetWindowLong = user32.SetWindowLongW if running_32bit else user32.SetWindowLongPtrW
              SetWindowLong.restype = WndProcType
              SetWindowLong.argtypes = [wintypes.HWND, wintypes.INT, WndProcType]
              
              GWL_WNDPROC = -4
              
              WM_KEYDOWN    = 0x100
              WM_KEYUP      = 0x101
              WM_SYSKEYDOWN = 0x104
              WM_SYSKEYUP   = 0x105
              WM_KILLFOCUS  = 0x8
              
              VK_SHIFT         = 0x10
              VK_CONTROL       = 0x11
              VK_MENU = VK_ALT = 0x12
              
              VK_LEFT  = 0x25
              VK_UP    = 0x26
              VK_RIGHT = 0x27
              VK_DOWN  = 0x28
              
              #-------------------------------------------------------------------------------
              
              class RSMT(object):
              
                  def __init__(self):
              
                      self.debug = True if 1 else False
              
                      self.this_script_name = inspect.getframeinfo(inspect.currentframe()).filename.split(os.sep)[-1].rsplit('.', 1)[0]
              
                      self.shift_pressed = self.ctrl_pressed = self.alt_pressed = False
              
                      self.altless_rectangular_select_mode_active = False
                      # if this mode is active, user doesn't have to hold Alt while pressing Shift+arrows in order to make a rectangular selection
              
                      self.new_editor1_wnd_proc_hook_for_SetWindowLong = WndProcType(self.new_editor1_wnd_proc_hook)
                      self.orig_editor1_wnd_proc = SetWindowLong(editor1.hwnd, GWL_WNDPROC, self.new_editor1_wnd_proc_hook_for_SetWindowLong)
              
                      self.new_editor2_wnd_proc_hook_for_SetWindowLong = WndProcType(self.new_editor2_wnd_proc_hook)
                      self.orig_editor2_wnd_proc = SetWindowLong(editor2.hwnd, GWL_WNDPROC, self.new_editor2_wnd_proc_hook_for_SetWindowLong)
              
                  def common_editor_wnd_proc_hook(self, hwnd, msg, wParam, lParam):
              
                      retval = True  # default to allowing something other than this code to handle this message
              
                      if msg in [WM_KEYDOWN, WM_SYSKEYDOWN]:
              
                          if   wParam == VK_SHIFT:   self.shift_pressed = True
                          elif wParam == VK_CONTROL: self.ctrl_pressed  = True
                          elif wParam == VK_ALT:     self.alt_pressed   = True
              
                          elif wParam in [ VK_LEFT, VK_UP, VK_RIGHT, VK_DOWN ]:
              
                              modifiers = ''
                              modifiers += 'SHIFT+' if self.shift_pressed else ''
                              modifiers += 'CTRL+'  if self.ctrl_pressed  else ''
                              modifiers += 'ALT+'   if self.alt_pressed   else ''
                              key = ''
                              key += 'LEFT'  if wParam == VK_LEFT  else ''
                              key += 'UP'    if wParam == VK_UP    else ''
                              key += 'RIGHT' if wParam == VK_RIGHT else ''
                              key += 'DOWN'  if wParam == VK_DOWN  else ''
                              self.print(modifiers + key)
              
                              if self.altless_rectangular_select_mode_active:
              
                                  if self.shift_pressed and not self.ctrl_pressed and not self.alt_pressed:
              
                                      retval = False  # allow no further processing of this message; it will be handled here
              
                                      if wParam   == VK_LEFT:  editor.charLeftRectExtend()
                                      elif wParam == VK_UP:    editor.lineUpRectExtend()
                                      elif wParam == VK_RIGHT: editor.charRightRectExtend()
                                      elif wParam == VK_DOWN:  editor.lineDownRectExtend()
              
                      elif msg in [WM_KEYUP, WM_SYSKEYUP]:
              
                          if wParam   == VK_SHIFT:   self.shift_pressed = False
                          elif wParam == VK_CONTROL: self.ctrl_pressed  = False
                          elif wParam == VK_ALT:     self.alt_pressed   = False
              
                      elif msg == WM_KILLFOCUS:
              
                          self.shift_pressed = self.ctrl_pressed = self.alt_pressed = False
              
                      return retval
              
                  def new_editor1_wnd_proc_hook(self, hwnd, msg, wParam, lParam):
                      retval = self.common_editor_wnd_proc_hook(hwnd, msg, wParam, lParam)
                      if retval: retval = self.orig_editor1_wnd_proc(hwnd, msg, wParam, lParam)
                      return retval
              
                  def new_editor2_wnd_proc_hook(self, hwnd, msg, wParam, lParam):
                      retval = self.common_editor_wnd_proc_hook(hwnd, msg, wParam, lParam)
                      if retval: retval = self.orig_editor2_wnd_proc(hwnd, msg, wParam, lParam)
                      return retval
              
                  def toggle(self):
                      editor.setEmptySelection(editor.getCurrentPos())
                      self.altless_rectangular_select_mode_active = not self.altless_rectangular_select_mode_active
                      self.mb('Alt-less column select mode is now {}'.format('active' if self.altless_rectangular_select_mode_active else 'inactive'))
              
                  def print(self, *args):
                      if self.debug:
                          #console.show()
                          print('RSMT:', *args)
              
                  def mb(self, msg, flags=0, title=''):  # a message-box function
                      return notepad.messageBox(msg, title if title else self.this_script_name, flags)
              
              #-------------------------------------------------------------------------------
              
              if __name__ == '__main__':
                  try:
                      rsmt
                  except NameError:
                      rsmt = RSMT()
                  rsmt.toggle()
              
              John Doe 1 1 Reply Last reply Reply Quote 1
              • Referenced by  Alan Kilborn Alan Kilborn 
              • John Doe 1
                John Doe 1 @Alan Kilborn last edited by

                @alan-kilborn Thanks for that! I still would like to know how to check if Notepad++ is closed or tabbed out of for future projects. Is there a function from Notepad, Editor or somewhere that can return whether or not Notepad++ is the active window? I would like to remedy this issue in my Script for satisfaction’s sake.

                Alan Kilborn PeterJones 2 Replies Last reply Reply Quote 0
                • Alan Kilborn
                  Alan Kilborn @John Doe 1 last edited by

                  @john-doe-1 said in PythonScript Toggleable Script?:

                  check if Notepad++ is closed

                  You scan do this with NOTIFICATION.SHUTDOWN in a notepad.callback. To see how this works if you don’t know, type notepad.callback into the editor and then invoke PythonScript’s context-sensitive help on it.

                  There’s also SCINTILLANOTIFICATION.FOCUSOUT which you could experiment with to see if it meets your needs. That one is an editor.callback.

                  can return whether or not Notepad++ is the active window?

                  I suppose you could try things with the windows API function GetForegroundWindow.

                  John Doe 1 1 Reply Last reply Reply Quote 2
                  • PeterJones
                    PeterJones @John Doe 1 last edited by

                    @john-doe-1 ,

                    Your paradigm is BAD and causing you problems.

                    Try this script out for size:

                    # encoding=utf-8
                    """in response to https://community.notepad-plus-plus.org/topic/22890/ and 22919
                    
                    alternate paradigm
                    """
                    from Npp import notepad, editor, SELECTIONMODE, STATUSBARSECTION
                    
                    try:
                        columnSelectMode
                        startPos
                        endPos
                    except NameError:
                        #console.show()
                        console.write('initialize toggle mode for the first time\n')
                        columnSelectMode = False
                        startPos = None
                        endPos = None
                    
                    if not columnSelectMode:
                        startPos = editor.getCurrentPos()
                        endPos = startPos
                        notepad.setStatusBar(STATUSBARSECTION.DOCTYPE, "In Rectangle/Column Selection Mode")
                    
                    else:
                        endPos = editor.getCurrentPos()
                        editor.setSel(startPos, endPos)
                        editor.setSelectionMode( SELECTIONMODE.RECTANGLE )
                    
                        # this overrides the statusbar.. but the refresh UI will overwrite that with default
                        notepad.setStatusBar(STATUSBARSECTION.DOCTYPE, "")
                    
                        # use the upcoming activateFile to refresh UI
                        #   otherwise, it doesn't _look_ like column/rectangle select)
                        notepad.activateFile(notepad.getCurrentFilename())
                    
                    columnSelectMode = not columnSelectMode
                    
                    #console.show()
                    #console.write("toggle selection mode to {}: {} .. {}\n".format(columnSelectMode, startPos, endPos))
                    
                    John Doe 1 1 Reply Last reply Reply Quote 1
                    • Referenced by  PeterJones PeterJones 
                    • John Doe 1
                      John Doe 1 @PeterJones last edited by

                      @peterjones the statement:

                      editor.setSelectionMode( SELECTIONMODE.RECTANGLE )

                      doesn’t seem to work, I tested it in a simple script with nothing else going on and this makes no changes to the selection mode, I’ve no idea why.

                      PeterJones 2 Replies Last reply Reply Quote 0
                      • PeterJones
                        PeterJones @John Doe 1 last edited by PeterJones

                        @john-doe-1 ,

                        That’s what I thought at first. But when I triggered the refresh (the activate file line), it updated and showed that it really did change what was shown to be the rectangle – that’s why I included that line in the script, and included comments to say it was needed to get it to show it’s a rectangle.

                        You can also tell that it was rectangle by just doing the editor.getSelectionMode() which returns a 1 (IIRC) for rectangle, or just do the copy then paste in the new location, and see it only grabbed the rectangle, not the full lines. Actually, I first saw it was working when I swapped to another tab and then back, and saw it was suddenly a rectangle instead of the stream it looked like.

                        John Doe 1 Alan Kilborn 2 Replies Last reply Reply Quote 1
                        • John Doe 1
                          John Doe 1 @PeterJones last edited by

                          @peterjones Thank you, I saw your reply to my other post also. My apologies, I’ll delete it and refrain from that in the future.

                          1 Reply Last reply Reply Quote 0
                          • Alan Kilborn
                            Alan Kilborn @PeterJones last edited by

                            @peterjones said in PythonScript Toggleable Script?:

                            editor.getSelectionMode() which returns a 1 (IIRC) for rectangle

                            That is indeed correct, and this is also available returning True/False: editor.selectionIsRectangle()

                            1 Reply Last reply Reply Quote 1
                            • PeterJones
                              PeterJones @John Doe 1 last edited by

                              @john-doe-1 ,

                              Since it apparently confused others, I will be more explicit about the example I gave above: it has a slightly different usage model than your explain seems to imply. If we read your usage model correctly, you want to have this script toggle the “column” mode, and you are then using shift+arrows to make the block selection live.

                              In my script, I figured: if you don’t want to faff around with ALT, then also don’t faff around with SHIFT. My usage model (for the script shown above) is “run script to start the column/rectangle selection; move cursor (either normal arrow or just click, since you’re already clicking the script on the toolbar); run script to end column/rectangle selection and then refresh the display so it looks like a column selection”.

                              But if you or your users insist on holding down the SHIFT, my script (above) will also work for that – it will look like a normal selection until the end when you run the script the second time, then it will convert the selection from normal to rectangular.

                              Which means, really, the script could be simplified to just run

                              from Npp import editor, notepad, SELECTIONMODE
                              editor.setSelectionMode(SELECTIONMODE.RECTANGLE)
                              notepad.activateFile(notepad.getCurrentFilename())
                              

                              Then the usage model would be “1) move your cursor to the start of where you want to select (via mouse or keyboard); 2) SHIFT+arrow or SHIFT+click to draw a normal selection; 3) run the script to convert the normal selection to a rectangle/column selection”. Fewer clicks. Call the script “convert active selection to rectangle” and be done with it.

                              PeterJones 1 Reply Last reply Reply Quote 0
                              • PeterJones
                                PeterJones @PeterJones last edited by PeterJones

                                @john-doe-1

                                So here are some example screenshot-videos of using the two

                                I assigned the ⇅ button to the “toggle” (the post that has the script with "alternate paradigm" in the comments).

                                For the ⇅ , I record the sequence

                                1. start selection with a click
                                2. click the ⇅ button to start the rectangle/column selection
                                3. end selection with a click
                                4. click the ⇅ button to end the rectangle/column selection, and convert it to visually be a rectangle selection
                                5. start new selection with a click
                                6. click the ⇅ button to start the rectangle/column selection
                                7. extend new selection with SHIFT+arrow
                                8. click the ⇅ button to end the rectangle/column selection, and convert it to visually be a rectangle selection

                                You will see that the status bar reflects the correct state of when you have clicked ⇅ the first time (so the script is in “active” mode), and that it clears itself when you do the second ⇅ (so it’s converted)

                                ---------------------

                                a1fa538a-4877-4745-ae14-de90d27d0b9d-image.png I assigned the other button to the “simplest” – the three-line version shown in my later post.

                                For the a1fa538a-4877-4745-ae14-de90d27d0b9d-image.png , I recorded the sequence

                                1. start the selection with a click
                                2. extend the selection with SHIFT+ARROW
                                3. convert the selection to rectangular by calling the 🛈 script

                                This doesn’t show the status, because the button is a one-time thing, not a “in the active mode” thing.

                                Either should work. Both avoid any low-level programming or callbacks – they just use standard, simple PythonScript calls.

                                The second has the benefit of being really simple, and doesn’t even need to save state. If you or your user want to convert a normal selection to rectangle, just click the button after making the selection. Don’t need to worry about the status bar. Easiest to use. Easiest to maintain. You can always decide to use that to change any stream selection into a rectangle, whether you were thinking “I need to select a rectangle” or not. Fewer clicks.

                                1 Reply Last reply Reply Quote 2
                                • Alan Kilborn
                                  Alan Kilborn last edited by

                                  So…all this is great, but I really don’t see an advantage to it (the whole concept), and this is why:

                                  • if you’re a dedicated keyboardist, you’d have to dive for the mouse in order to hit the toolbar button to change the mode, spoiling being dedicated to the keyboard (where you could just add Alt to your Shift+arrows movement to get a column block).

                                  • if you’re not in love with keyboard-only actions, use the mouse to select text via click and drag; if you start this as a stream selection, you can add a press of Alt to it while you are dragging (can even just tap-n-release Alt) in order to change the selection to a column block type.

                                  But…people will want what they will want, and that’s ok. :-)

                                  1 Reply Last reply Reply Quote 2
                                  • John Doe 1
                                    John Doe 1 last edited by

                                    Okay, going back to what the this thread was originally about, my toggle function is not working as I thought it would. If I go to click it to toggle it off, I get the following messagebox text:

                                    “Another script is currently running. Running two scripts at the same time could produce unpredictable results, and is therefore disabled.”

                                    I’m wondering how I can make it so that the user can break the infinite loop in my script by clicking on the button for the script again, essentially making it toggleable. Anyone got an idea? It would be much appreciated.

                                    Alan Kilborn PeterJones 2 Replies Last reply Reply Quote 0
                                    • Alan Kilborn
                                      Alan Kilborn @John Doe 1 last edited by

                                      @john-doe-1 said in PythonScript Toggleable Script?:

                                      how I can make it so that the user can break the infinite loop in my script

                                      Possible solution: Don’t put an infinite loop in the script in the first place?

                                      John Doe 1 1 Reply Last reply Reply Quote 0
                                      • PeterJones
                                        PeterJones @John Doe 1 last edited by

                                        @john-doe-1 said in PythonScript Toggleable Script?:

                                        I’m wondering how I can make it so that the user can break the infinite loop in my script by clicking on the button for the script again

                                        By writing it without an infinite loop. The three line script I supplied is all you need to be able to easily convert a normal selection to a rectangle/column selection after the selection is made. Or the ⇅ script I showed you will allow a toggle-on/toggle-off without requiring an infinite loop.

                                        I cannot say it any more plainly.

                                        1 Reply Last reply Reply Quote 1
                                        • John Doe 1
                                          John Doe 1 @Alan Kilborn last edited by John Doe 1

                                          @alan-kilborn @PeterJones The reason for the infinite loop is that I want to make the script act like a toggle that works for not only arrow keys but also clicking and dragging for selection. I have the following statement in my infinite loop (among others):

                                          editor.setSelectionMode(SELECTIONMODE.RECTANGLE)

                                          This is the only way to be able to click and drag rectangle select persistently, it works for arrow key selection as well, only thing is if it is not in an infinite loop then that functionality no longer works.

                                          Edit: Is there a function that can check if another PythonScript is being launched? That would be a good way to break the loop.

                                          PeterJones 2 Replies Last reply Reply Quote 0
                                          • PeterJones
                                            PeterJones @John Doe 1 last edited by PeterJones

                                            @john-doe-1

                                            Did you not try my scripts? Because both ⇅ and the “simplest” will work, whether you use the keyboard or clicking and dragging. With ⇅ , click where you want to start the selection, then run the script, then click-and-drag to the end of the selection, then run the script, and it will convert that click-and-drag-selection to rectangle. With the “simplest” script, just click-and-drag your selection, then run the script, and it will convert that click-and-drag-selection to rectangle.

                                            The infinite loop will not work for you. Stop trying to make it work. It is the wrong design. Every one of the problems you have run across has proven that to everyone (except you, who seems unwilling to accept an alternate solution that works better).

                                            I have already written multiple scripts, and shown you how it works, in words and in video. If you are unwilling to go this route, I cannot help you beyond this. I am sorry. Good luck.

                                            John Doe 1 1 Reply Last reply Reply Quote 1
                                            • First post
                                              Last post
                                            Copyright © 2014 NodeBB Forums | Contributors