Community
    • Login

    Python / Lua Script: Detect if a modifier is pressed when running a script

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    60 Posts 5 Posters 55.0k Views
    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.
    • YaronY
      Yaron
      last edited by

      Hello Claudia,

      After a quick look in your script:
      Is using ctypes the only way to detect if a modifier was pressed?
      Can’t it be achieved with some keys module?

      And allow me to clarify:
      I want to know if a modifier was pressed when starting the script. NOT when it’s already running.

      Thanks again.
      BR

      1 Reply Last reply Reply Quote 0
      • Claudia FrankC
        Claudia Frank
        last edited by

        Hi Yaron,

        no, unfortunately neither scintilla nor notepad provide a keyup/keydown event which could be
        consumed.
        The only way, I know, is using ctypes and I think python modules which would offer this would also
        call win api to get the events.

        And allow me to clarify:
        I want to know if a modifier was pressed when starting the script. NOT when it’s already running.

        So you need to have the keyboard logger registered within startup.py ;-)

        Cheers
        Claudia

        1 Reply Last reply Reply Quote 0
        • YaronY
          Yaron
          last edited by

          Hello Claudia,

          Thank you for the detailed explanation. I appreciate it.

          So you need to have the keyboard logger registered within startup.py

          I haven’t thoroughly learned the script yet.

          If I have a bool altPressed in startup.py, I’d need to set it to false on Alt-UP only after some timeout. Is that correct?

          BR

          Claudia FrankC 2 Replies Last reply Reply Quote 0
          • Claudia FrankC
            Claudia Frank @Yaron
            last edited by

            Yaron,
            in your case, if I understand it correctly, I would use

                if msg in [WM_KEYDOWN, WM_SYSKEYDOWN]: 
            

            to set the boolean true and

                elif msg in [WM_KEYUP,WM_SYSKEYUP]: 
            

            to set it to false

            and you need to handle the cases when alt together with other modifiers is pressed.

            Cheers
            Claudia

            1 Reply Last reply Reply Quote 0
            • Claudia FrankC
              Claudia Frank @Yaron
              last edited by

              @Yaron

              Detecting a right-click would be better (i.e. execute the script on both left/right-click and detect the button).

              sorry, I missed this one.
              So you do have defined a button in the toolbar, correct?
              And when clicking with left mouse = normal run but when clicking with right mouse then advanced run?

              Cheers
              Claudia

              1 Reply Last reply Reply Quote 0
              • YaronY
                Yaron
                last edited by

                If I have a bool altPressed in startup.py, I’d need to set it to false on Alt-UP only after some timeout. Is that correct?

                Or setting it to false only in the altDependentScript.
                I’ll think about it.

                You’ve been very helpful as always. :)
                Thank you very much.

                BR

                Claudia FrankC 1 Reply Last reply Reply Quote 1
                • Claudia FrankC
                  Claudia Frank @Yaron
                  last edited by

                  Yaron, in case you want to get the mouse clicks see here.

                  Cheers and good night
                  Claudia

                  1 Reply Last reply Reply Quote 0
                  • YaronY
                    Yaron
                    last edited by

                    Hello again Claudia,

                    Detecting a right-click would be better (i.e. execute the script on both left/right-click and detect the button).
                    …
                    sorry, I missed this one.
                    So you do have defined a button in the toolbar, correct?
                    And when clicking with left mouse = normal run but when clicking with right mouse then advanced run?

                    That’s correct.

                    in case you want to get the mouse clicks see here.

                    Getting the specific command/button position would be too much. :)

                    Thanks again. Highly appreciated.

                    Good night.

                    1 Reply Last reply Reply Quote 0
                    • dailD
                      dail
                      last edited by

                      As far as LuaScript goes you’d probably be alot more limited as you have to use what Scintilla/Notepad++ provides which you can catch double clicks and get the modifier keys but don’t think there’s a way to distinguish between left or right click. You could also register the same function with more than one keyboard shortcut, such as “F5”, “Ctrl+F5”, “Alt+F5”, etc…but this is not ideal.

                      Python gives more possibilities as far as interacting with the OS so this is probably your better option depending what you need exactly.

                      1 Reply Last reply Reply Quote 0
                      • YaronY
                        Yaron
                        last edited by

                        Hello @dail,

                        Thank you for the additional info. I appreciate it.

                        If I understand it correctly, SCN_DOUBLECLICK is only relevant to the editor.
                        Double-click could have been useful, but I need it for a command or a toolbar button.

                        And thanks again for the great Lua Script plugin.
                        BTW, Ctrl+left-click to open the file instead of running the script could be a nice addition to Lua Script.
                        What do you think?

                        BR

                        1 Reply Last reply Reply Quote 0
                        • dailD
                          dail
                          last edited by

                          but I need it for a command or a toolbar button.

                          Oh! Now I see what you are doing. In this case PythonScript is your best option.

                          Ctrl+left-click to open the file instead of running the script could be a nice addition to Lua Script.

                          Honestly I don’t even know if would be possible. Since individual functions are registered instead of files, there is nothing that associates where a function is defined.

                          1 Reply Last reply Reply Quote 0
                          • YaronY
                            Yaron
                            last edited by

                            Hello @dail,

                            Oh! Now I see what you are doing. In this case PythonScript is your best option.

                            A simple example for what I meant:
                            A script TypeLetter types a letter in the editor. Using Customize Toolbar, I’ve assigned a toolbar button to the script.
                            I want it to normally type A but if Alt is down when pressing the toolbar button type B.

                            Claudia’s suggestion is a good option, but I think I’d have to use some timeout.
                            Using another script Toggle TypeLetter Mode is another option which should work well in Lua Script too.

                            Since individual functions are registered instead of files…

                            I didn’t think about that. :)

                            Thanks again.
                            BR

                            Claudia FrankC 1 Reply Last reply Reply Quote 0
                            • Claudia FrankC
                              Claudia Frank @Yaron
                              last edited by

                              Hello Yaron

                              a quick script about what I meant with mouse click capture.

                              In one script you intercept the window proc (needs to be called first, startup.py might be good choice)

                              from ctypes import windll, byref, wintypes, WINFUNCTYPE, Structure, sizeof
                              
                              WndProcType = WINFUNCTYPE(wintypes.LONG,
                                                        wintypes.HWND,
                                                        wintypes.UINT,
                                                        wintypes.WPARAM,
                                                        wintypes.LPARAM)
                              GWL_WNDPROC = -4
                              
                              WNDENUMPROC = WINFUNCTYPE(wintypes.BOOL,
                                                        wintypes.HWND,
                                                        wintypes.LPARAM)
                              
                              
                              _g = globals()
                              CAPTURE_MOUSE_CLICK = _g.get('CAPTURE_MOUSE_CLICK', False)
                              OLD_WND_PROC = _g.get('OLD_WND_PROC', None)
                              LEFT_MOUSE_CLICK_MOD = False
                              
                              class Hook():
                              
                                  def EnumCallback(self, hwnd, lparam):
                                      curr_class = (wintypes.WCHAR * 256)()
                                      windll.user32.GetClassNameW(hwnd, curr_class, 256)
                                      if curr_class.value.lower() == 'toolbarwindow32':
                                          self.toolbar_handle = hwnd
                                          return False
                                      return True
                              
                                  def __init__(self):
                                      self.toolbar_handle = None
                                      parent = windll.user32.FindWindowA('Notepad++',None)
                                      windll.user32.EnumChildWindows(parent, WNDENUMPROC(self.EnumCallback), 0)
                              
                                      self.oldWndProc = None
                              
                                      self.SHIFT_PRESSED = False
                                      self.CTRL_PRESSED = False
                                      self.ALT_PRESSED = False
                              
                                  def register(self):
                                      if self.toolbar_handle:
                                          self.new_wnd_proc = WndProcType(self.sciWndProc)
                                          windll.kernel32.SetLastError(0)
                                          self.oldWndProc = windll.user32.SetWindowLongA(self.toolbar_handle, GWL_WNDPROC, self.new_wnd_proc)
                              
                                          if self.oldWndProc:
                                              global OLD_WND_PROC
                                              OLD_WND_PROC = self.oldWndProc
                                          else:
                                              _err = 'GetLastError:{}'.format(windll.kernel32.GetLastError())
                                              notepad.messageBox('Could not register hook:\n{}\n'.format(_err) +
                                                                  'Shortcuts won\'t work',
                                                                  'Register Hook Failure', 0)
                                      else:
                                          console.write('no toolbar_handle found')
                              
                                  def unregister(self):
                                      if OLD_WND_PROC:
                                          self.oldWndProc = OLD_WND_PROC
                                          windll.kernel32.SetLastError(0)
                                          dummy = windll.user32.SetWindowLongA(self.toolbar_handle, GWL_WNDPROC, self.oldWndProc)
                                          if not dummy:
                                              _err = 'GetLastError:{}'.format(windll.kernel32.GetLastError())
                                              notepad.messageBox('Could not unregister hook:\n{}\n'.format(_err) +
                                                                  'It is recommended to save data and restart npp to prevent data loss',
                                                                  'Unregister Hook Failure', 0)
                              
                                      else:
                                          console.write('ERROR no saved window proc found')
                              
                                  def sciWndProc(self, hWnd, msg, wParam, lParam):
                                      if msg == 0x0201: # WM_LBUTTONDOWN
                                          global LEFT_MOUSE_CLICK_MOD
                                          if wParam == 5: # 0x0001 LButton + 0x0004 Shift
                                              console.write('left mouse click while holding shift\n')
                                              LEFT_MOUSE_CLICK_MOD = True
                                          else:
                                              console.write('left mouse click\n')
                                              LEFT_MOUSE_CLICK_MOD = False
                              
                                      return windll.user32.CallWindowProcA (self.oldWndProc, hWnd, msg, wParam, lParam)
                              
                              # -----------------------------------------------------------------------------
                              _hook = Hook()
                              if not CAPTURE_MOUSE_CLICK:
                                  _hook.register()
                                  CAPTURE_MOUSE_CLICK = True
                                  console.write('registered')
                              else:
                                  _hook.unregister()
                                  CAPTURE_MOUSE_CLICK = False
                                  console.write('unregistered')
                              

                              in another script something like

                              import datetime
                              print datetime.datetime.now()
                              print 'LEFT_MOUSE_CLICK_MOD:{}'.format(LEFT_MOUSE_CLICK_MOD)
                              

                              The first script gets the handle of the toolbar and hooks it message queue.
                              Once you click a button the global variable is set and the second script gets executed.

                              Cheers
                              Claudia

                              1 Reply Last reply Reply Quote 0
                              • YaronY
                                Yaron
                                last edited by

                                Hello Claudia,

                                Sorry for the late reply. I was not by my PC.
                                And thank you for the beautiful script. I appreciate your kindness.

                                Now that you capture both the click and the modifier, there’s no need to use timeout and it works like a charm.

                                Two follow-up questions with your permission:

                                1. Customize Toolbar inserts a separator on pressing Shift and left-clicking a toolbar button.
                                  Which module should I import in order to use GetKeyState() and detect Alt?

                                2. The little programming experience I have is from Firefox.
                                  In that browser adding a right-click functionality to a toolbar button is done as follows:

                                • Get A specific toolbar button object.
                                • Add a click listener to that button and assign your own action to right-click.

                                I suppose this procedure would be more complex in NPP.

                                Actually you can replace in your script

                                if wParam == 5: # 0x0001 LButton + 0x0004 Shift
                                

                                with

                                if wParam == 3: # 0x0001 LButton + 0x0002 MK_RBUTTON
                                

                                and then if you hold the right button down and left-click - you can execute the script in “alternative mode”.


                                Another simple (and not as elegant) way to run a script in two modes:
                                In startup (py or lua) add testScriptDefaultMode = True.
                                Create a script Toggle TestScript Default Mode containing two lines:

                                global testScriptDefaultMode 
                                testScriptDefaultMode = not testScriptDefaultMode 
                                

                                In TestScript check if testScriptDefaultMode:.

                                Assign a simple shortcut (e.g. Alt+Spacebar) to Toggle TestScript Default Mode and use it to quickly toggle the modes…


                                Thanks again.
                                Have a nice weekend.

                                Claudia FrankC 1 Reply Last reply Reply Quote 0
                                • Claudia FrankC
                                  Claudia Frank @Yaron
                                  last edited by Claudia Frank

                                  @Yaron

                                  Customize Toolbar inserts a separator on pressing Shift and left-clicking a toolbar button.
                                  Which module should I import in order to use GetKeyState() and detect Alt?

                                  The function is also called GetKeyState and is in user32.dll.
                                  You need to provide the virtual key code to get the state, so something like

                                  alt_state = windll.user32.GetKeyState(0x12)
                                  

                                  but using wine, I was not able to press alt and do left click with mouse.
                                  Would be interesting to see if this works on Windows.

                                  The little programming experience I have is from Firefox.
                                  In that browser adding a right-click functionality to a toolbar button is done as follows:
                                  Get A specific toolbar button object.
                                  Add a click listener to that button and assign your own action to right-click.
                                  I suppose this procedure would be more complex in NPP.

                                  I think so too. Out of my mind, you would need to find notepad++ window handle,
                                  enumerate child windows to get toolbar, enumerate toolbar child windows to get
                                  the button in question (not sure how to identify), register a function which hooks into
                                  buttons message queue and I don’t know what happens if another program accesses toolbar
                                  to modify (or add/delete) a button?? I’m pretty sure it can be done but I’m not convinced
                                  it should be done.

                                  Another possible solution to run script in alternate mode would be to use a
                                  inputbox (prompt method) ;-) (also not as elegant but most painless version) :-D

                                  Cheers
                                  Claudia

                                  1 Reply Last reply Reply Quote 1
                                  • YaronY
                                    Yaron
                                    last edited by

                                    Hello Claudia,

                                    The function is also called GetKeyState and is in user32.dll.

                                    Great. Thank you.

                                    but using wine, I was not able to press alt and do left click with mouse.
                                    Would be interesting to see if this works on Windows.

                                    alt_state = windll.user32.GetKeyState(0x12)
                                    if alt_state < 0:
                                    

                                    Works perfectly.

                                    Out of my mind, you would need to find notepad++ window handle…

                                    Your confirmation that this is indeed rather complex is important.

                                    Another possible solution to run script in alternate mode would be to use a
                                    inputbox

                                    So I have multiple choices now. :)
                                    Being a “mouse-oriented” user, your script detecting if the right button was down when left-clicking would probably fit me best.

                                    Thanks again for your time and clever ideas. I do appreciate it.
                                    BR

                                    Scott SumnerS 1 Reply Last reply Reply Quote 1
                                    • Scott SumnerS
                                      Scott Sumner @Yaron
                                      last edited by

                                      @Yaron and @Claudia-Frank

                                      Oftentimes I find myself writing a Pythonscript that either acts on text above the caret or below the caret. So I need a way to tell it which way to go. An input box would work, but it slows down one’s “workflow”. Often it is an alternate but related keypress (e.g. F10 runs functionality above the caret, Shift+F10 for below the caret) that I want to use. So what I’ve been doing is having 3 scripts, one “common” one, and two others. The two others are tied to F10 and Shift+F10, and call the common script, telling it the direction.

                                      So I just wanna say that this thread has gotten me thinking about how to do this cleaner; haven’t done anything about it yet, but definitely thinkin’… :-D

                                      Alan KilbornA 1 Reply Last reply Reply Quote 0
                                      • YaronY
                                        Yaron
                                        last edited by

                                        Hello Scott,

                                        It’s good to know an experienced “Pythoneer” like yourself have been coping with this issue.
                                        Our Claudia is really clever, isn’t she? :)

                                        Please share with us your preferred solution.

                                        Best regards.

                                        1 Reply Last reply Reply Quote 1
                                        • Alan KilbornA
                                          Alan Kilborn @Scott Sumner
                                          last edited by

                                          @Scott-Sumner
                                          I don’t know that this thread will be of any help for that kind of key differentiation. I do see how it might work with a toolbar button assigned to a pythonscript, though, but this seems like it isn’t what you want.

                                          1 Reply Last reply Reply Quote 0
                                          • YaronY
                                            Yaron
                                            last edited by

                                            Hello Claudia and all,

                                            Thanks again for the script. I appreciate it.

                                            I’ve slightly modified it so that right-clicking a toolbar button would execute its command.
                                            Emulating the regular left-click behavior:

                                            • When the right button is down, the toolbar button is “checked”.
                                              default
                                            • The command is executed only when the right button is released.

                                            If anyone can think of a better way to achieve that behavior, please let me know.


                                            from ctypes import windll, byref, wintypes, WINFUNCTYPE, Structure, sizeof
                                            
                                            WndProcType = WINFUNCTYPE(wintypes.LONG,
                                                                      wintypes.HWND,
                                                                      wintypes.UINT,
                                                                      wintypes.WPARAM,
                                                                      wintypes.LPARAM)
                                                                      
                                            GWL_WNDPROC = -4
                                            
                                            WNDENUMPROC = WINFUNCTYPE(wintypes.BOOL,
                                                                      wintypes.HWND,
                                                                      wintypes.LPARAM)
                                            
                                            
                                            _g = globals()
                                            CAPTURE_MOUSE_CLICK = _g.get('CAPTURE_MOUSE_CLICK', False)
                                            OLD_WND_PROC = _g.get('OLD_WND_PROC', None)
                                            
                                            RIGHT_CLICK_MODE = False
                                            REAL_RIGHT_UP = False
                                            
                                            class Hook():
                                            
                                                def EnumCallback(self, hwnd, lparam):
                                                    curr_class = (wintypes.WCHAR * 256)()
                                                    windll.user32.GetClassNameW(hwnd, curr_class, 256)
                                                    if curr_class.value.lower() == 'toolbarwindow32':
                                                        self.toolbar_handle = hwnd
                                                        return False
                                                        
                                                    return True
                                            
                                                def __init__(self):
                                                    self.toolbar_handle = None
                                                    parent = windll.user32.FindWindowA('Notepad++', None)
                                                    windll.user32.EnumChildWindows(parent, WNDENUMPROC(self.EnumCallback), 0)
                                            
                                                    self.oldWndProc = None
                                            
                                                def register(self):
                                                    if self.toolbar_handle:
                                                        self.new_wnd_proc = WndProcType(self.sciWndProc)
                                                        windll.kernel32.SetLastError(0)
                                                        self.oldWndProc = windll.user32.SetWindowLongA(self.toolbar_handle, GWL_WNDPROC, self.new_wnd_proc)
                                            
                                                        if self.oldWndProc:
                                                            global OLD_WND_PROC
                                                            OLD_WND_PROC = self.oldWndProc
                                                        else:
                                                            _err = 'GetLastError:{}'.format(windll.kernel32.GetLastError())
                                                            notepad.messageBox('Could not register hook:\n{}\n'.format(_err) +
                                                                                'Right-click mode won\'t work',
                                                                                'Register Hook Failure', 0)
                                                    else:
                                                        console.write('no toolbar_handle found')
                                            
                                                def unregister(self):
                                                    if OLD_WND_PROC:
                                                        self.oldWndProc = OLD_WND_PROC
                                                        windll.kernel32.SetLastError(0)
                                                        dummy = windll.user32.SetWindowLongA(self.toolbar_handle, GWL_WNDPROC, self.oldWndProc)
                                                        if not dummy:
                                                            _err = 'GetLastError:{}'.format(windll.kernel32.GetLastError())
                                                            notepad.messageBox('Could not unregister hook:\n{}\n'.format(_err) +
                                                                                'It is recommended to save data and restart npp to prevent data loss',
                                                                                'Unregister Hook Failure', 0)
                                            
                                                    else:
                                                        console.write('ERROR no saved window proc found')
                                            
                                            
                                            	def sciWndProc(self, hWnd, msg, wParam, lParam):
                                            		global REAL_RIGHT_UP
                                            		global RIGHT_CLICK_MODE
                                            		if msg == 0x0204:		# WM_RBUTTONDOWN.
                                            			REAL_RIGHT_UP = False
                                            			windll.user32.mouse_event(0x0002, 0, 0, 0, 0)	# Left down. WM_RBUTTONUP is sent although the right button is still down.
                                            		elif msg == 0x0205:	# WM_RBUTTONUP.
                                            			if REAL_RIGHT_UP:		# Another WM_RBUTTONUP is sent when the right button is released. Process it now.
                                            				RIGHT_CLICK_MODE = True
                                            				windll.user32.mouse_event(0x0004, 0, 0, 0, 0)	# Left up.
                                            				windll.user32.SetTimer(hWnd, 1, 1000, 0);	# 1 == TIMER_ID.
                                            
                                            			REAL_RIGHT_UP = True
                                            		elif msg == 0x0113 and wParam == 1:		# WM_TIMER.
                                            			RIGHT_CLICK_MODE = False
                                            			windll.user32.KillTimer(hWnd, 1);
                                            
                                            		return windll.user32.CallWindowProcA (self.oldWndProc, hWnd, msg, wParam, lParam)
                                            # -----------------------------------------------------------------------------
                                            
                                            _hook = Hook()
                                            if not CAPTURE_MOUSE_CLICK:
                                                _hook.register()
                                                CAPTURE_MOUSE_CLICK = True
                                                console.write('registered')
                                            else:
                                                _hook.unregister()
                                                CAPTURE_MOUSE_CLICK = False
                                                console.write('unregistered')
                                            

                                            Copy and paste the code in startup.py.
                                            In a script you want to run in two modes, (i.e. left-click and right-click) add modeRightClick = RIGHT_CLICK_MODE and handle it accordingly.
                                            Since a 1 Sec timer is set to revert RIGHT_CLICK_MODE to False, it’s recommended to add modeRightClick = RIGHT_CLICK_MODE at the beginning of the script.

                                            Best regards.

                                            Claudia FrankC 2 Replies Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors