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.
    • 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
                        • Claudia FrankC
                          Claudia Frank @Yaron
                          last edited by

                          @Yaron

                          so you are concerned not seeing the pressed/checked button if right clicking on it, aren’t you?

                          Cheers
                          Claudia

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

                            @Yaron

                            here my solution, not fully error proved and just added without any structure
                            but I guess you get the point.

                            You need to import another 4 types from ctypes (pointer, c_ubyte, c_int, c_ulong)
                            The changed part is

                            def sciWndProc(self, hWnd, msg, wParam, lParam):
                                if msg == 0x0201: #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')
                                        
                                elif msg == 0x0204:
                                        class TBBUTTON(Structure):
                                            _fields_ = [
                                                        ('iBitmap', c_int),
                                                        ('idCommand', c_int),
                                                        ('fsState', c_ubyte),
                                                        ('fsStyle', c_ubyte),
                                                        ('bReserved', c_ubyte * 6), # 2 if win32
                                                        ('dwData', c_ulong),
                                                        ('iString', c_int),
                                                       ]
                                            
                                        LEFT_MOUSE_CLICK_MOD = False
                                        _point = wintypes.POINT()
                                        result = windll.user32.GetCursorPos(byref(_point))
                                        if result:
                                            _hwnd = windll.user32.WindowFromPoint(_point)
                                            windll.user32.ScreenToClient(_hwnd,pointer(_point))
                                            button_id = windll.user32.SendMessageA(_hwnd,
                                                                                   1024+69, # TB_HITTEST(WM_USER + 69)
                                                                                   0, 
                                                                                   pointer(_point))
                            
                                            
                                            _tbbutton = TBBUTTON()
                                            bool = windll.user32.SendMessageA(_hwnd,
                                                                              1024+23, # TB_GETBUTTON(WM_USER + 23))
                                                                              button_id, 
                                                                              byref(_tbbutton))
                                            
                                            
                                            if bool:
                                                pressed = windll.user32.SendMessageA(_hwnd,
                                                                                     1024+2, # TB_CHECKBUTTON(WM_USER + 2)
                                                                                     _tbbutton.idCommand, 
                                                                                     True)
                            
                                return windll.user32.CallWindowProcA (self.oldWndProc, hWnd, msg, wParam, lParam)
                            

                            Seems to work on linux, so should work on windows as well.

                            Cheers
                            Claudia

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

                              Hello Claudia,

                              Thank you very much for the new code. I do appreciate that.

                              I’m afraid we had some misunderstanding here.
                              With your permission, let’s start from scratch. :)

                              As I’ve mentioned in the first post here, my preferred solution is: right-click a toolbar button to execute its command in “alternative mode”.

                              A simple example: Select and Find Next and Select and Find Previous.
                              I want to have one toolbar button: Select and Find Next/Previous. - Left-clicking the button would find Next and right-clicking it would find Previous.

                              My code (that is: your code slightly modified) works perfectly well.
                              The button is “checked” on right-click and the command is executed on right-up (as the regular left-click behavior).

                              I just meant that my implementation is a bit tricky and I’d appreciate a better solution.
                              windll.user32.mouse_event(0x0002, 0, 0, 0, 0) # Left down. invokes a WM_RBUTTONUP message although the right button is still down.
                              And I want the command to be executed only when the right button is up…

                              Allow me to ask you to run my version and have a quick look at the code. You’ll get it very quickly. :)
                              Thank you.

                              I suppose your new code should make the button “checked”. It does that but the command is not executed.
                              Again: some misunderstanding.


                              I assume the following shorter version is adequate If I put the code in startup.py and I’m not interested in toggling the right-click capture. Is that correct?

                              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)
                              
                              RIGHT_CLICK_MODE = False
                              REAL_RIGHT_UP = False
                              
                              class ToolbarRightClick():
                              
                              	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 registerRightClick(self):
                              		if self.toolbar_handle:
                              			self.new_wnd_proc = WndProcType(self.sciWndProc)
                              			self.oldWndProc = windll.user32.SetWindowLongA(self.toolbar_handle, GWL_WNDPROC, self.new_wnd_proc)
                              			msgConsole = "Toolbar Right-Click Mode Registered.\n\n"
                              		else:
                              			msgConsole = "Error Registering Toolbar Right-Click Mode.\n\n"
                              		
                              		console.write(msgConsole)
                              
                              	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)
                              
                              # -------------------------------------------------------------------------------------------------------------
                              
                              _toolbarRightClick = ToolbarRightClick()
                              _toolbarRightClick.registerRightClick()
                              

                              Thanks again.
                              Have a great day.

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

                                @Yaron

                                I guess I see now but this misunderstanding shows a way to solve it.
                                When you get the button command id (_tbbutton.idCommand) you are able
                                to execute it via notepads menuCommand function like

                                notepad.menuCommand(_tbbutton.idCommand)
                                

                                so my latest example with the logic like this

                                global RIGHT_CLICK_MODE
                                if msg == 0x0201: # left
                                    RIGHT_CLICK_MODE = False
                                    ...
                                        
                                elif msg == 0x0204:
                                    RIGHT_CLICK_MODE = True
                                    ...
                                    pressed = windll.user32.SendMessageA(self.toolbar_hwnd,
                                                                         1024+2, # TB_CHECKBUTTON(WM_USER + 2)
                                                                         _tbbutton.idCommand, 
                                                                         True)  
                                    # execute script
                                    notepad.menuCommand(_tbbutton.idCommand)
                                                                                     
                                elif msg == 0x0205:
                                    RIGHT_CLICK_MODE = False
                                    pressed = windll.user32.SendMessageA(self.toolbar_hwnd,
                                                                         1024+2, # TB_CHECKBUTTON(WM_USER + 2)
                                                                         _tbbutton.idCommand, 
                                                                         False) 
                                

                                should do what you want.

                                Cheers
                                Claudia

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

                                  Hello Claudia,

                                  That’s brilliant. Thanks again for your time.

                                  If you want the command executed on right-up, move

                                  RIGHT_CLICK_MODE = True
                                  # execute script
                                  notepad.menuCommand(_tbbutton.idCommand)
                                  

                                  to elif msg == 0x0205:.

                                  Also, you can use windll.user32.mouse_event(0x0002, 0, 0, 0, 0) without a timer.

                                  	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.
                                  
                                  			REAL_RIGHT_UP = True
                                  		elif REAL_RIGHT_UP and msg == 0x0201:		# WM_LBUTTONDOWN.
                                  			RIGHT_CLICK_MODE = False
                                  
                                  		return windll.user32.CallWindowProcA (self.oldWndProc, hWnd, msg, wParam, lParam)
                                  

                                  Have a nice weekend.

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

                                    Hello Claudia,

                                    I had a closer look at your code and it’s really nice. Thanks again.

                                    Getting the button ID is certainly the “proper” approach.
                                    For the record, here is a shorter mouse_event implementation.

                                    	def sciWndProc(self, hWnd, msg, wParam, lParam):
                                    		global RIGHT_CLICK_MODE
                                    		if msg == 0x0204:		# WM_RBUTTONDOWN.
                                    			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 RIGHT_CLICK_MODE:		# Another WM_RBUTTONUP is sent when the right button is released. Send "Left up" now.
                                    				windll.user32.mouse_event(0x0004, 0, 0, 0, 0)	# Left up.
                                    			else:	# The right button is still down.
                                    				RIGHT_CLICK_MODE = True
                                    		elif RIGHT_CLICK_MODE and msg == 0x0201:		# WM_LBUTTONDOWN.
                                    			RIGHT_CLICK_MODE = False
                                    
                                    		return windll.user32.CallWindowProcA (self.oldWndProc, hWnd, msg, wParam, lParam)
                                    

                                    This is PythonScript console when using NPP in a RTL layout.
                                    default

                                    Reading the LTR output in a RTL layout is obviously inconvenient.

                                    @dail has kindly added an option to force LuaScript console to LTR.
                                    https://github.com/dail8859/LuaScript/issues/6#issuecomment-270676744
                                    https://github.com/dail8859/LuaScript/commit/8fed28f8ffe494c5e68a02010fddcdac4b4d3f74

                                    Do you think it would be possible to force PythonScript console to LTR using some code in startup.py?
                                    Or would it require some modifications in PythonScript source files?

                                    If and when you have some time to think about it, I’d be grateful.

                                    Best regards.

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

                                      Hello Yaron,

                                      if dails commit solves your issue with lua then I don’t see why we could get this to work with python script.
                                      The hard work to find out what needs to be done has been already done by @dail, thank you ;-).

                                      So we need to get the window hwnd from python script console (and what about the inputbox?)
                                      and change the style parameter for that window. Doesn’t sound undoable.

                                      Obviously I tend to solve using a python script but if you prefer I will see what needs to be changed in plugin source in order
                                      to make it happen but compiling and testing needs to be done on your site.

                                      Cheers
                                      Claudia

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

                                        Yaron,
                                        Am I right to assume that you do not only need RTL layout but also RTL reading, correct?
                                        Or do you really want to have LTR layout in python script while using RTL layout in npp?

                                        Cheers
                                        Claudia

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

                                          I’m talking about something like this

                                          Cheers
                                          Claudia

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

                                            Hello Claudia,

                                            Thank you for your patience and kindness. Highly appreciated.
                                            And allow me to thank @dail again for LuaScript in general and for the console issue in particular.

                                            I assumed it was possible to change PythonScript console to LTR but I preferred to get the right direction by consulting with the expert first. :)
                                            And, as usual, you already have the solution.

                                            (and what about the inputbox?)

                                            The InputBox in PythonScript is LTR even when NPP layout is RTL (so it doesn’t have to be modified).

                                            Obviously I tend to solve using a python script…

                                            That would be great.

                                            Am I right to assume that you do not only need RTL layout but also RTL reading, correct?

                                            You can change NPP text direction whether the layout is LTR or RTL (View -> Text Direction LTR/RTL).

                                            Or do you really want to have LTR layout in python script while using RTL layout in npp?

                                            Indeed.
                                            The console’s frame and input are always LTR. The problem is the output.

                                            Looking at your screenshot, I understand the the issue should be further clarified. :)

                                            This is NPP in RTL layout with LuaSceipt console in LTR (perfect).
                                            default

                                            And this is NPP in RTL layout with PythonSceipt console in LTR (frame and input) and RTL (output - confusing).
                                            default

                                            I’d like to see the output as you see it in LTR layout.

                                            This may further complicate the issue but OTOH it might help.
                                            The status bar in both screenshots is LTR.
                                            As I wrote to dail, I added the following line in StatusBar.cpp:

                                            // Change to LTR. "WS_CHILD & (~WS_EX_LAYOUTRTL)" above won't work because WS_EX_LAYOUTRTL is added AFTER the bar has been created and inherited the RTL layout.
                                            ::SetWindowLongPtr(_hSelf, GWL_EXSTYLE, ::GetWindowLongPtr(_hSelf, GWL_EXSTYLE) & (~WS_EX_LAYOUTRTL)); 
                                            

                                            after

                                            if (!_hSelf)
                                            		throw std::runtime_error("StatusBar::init : CreateWindowEx() function return null");
                                            

                                            So getting the console’s handle (or only the output’s?) and changing the WS_EX_LAYOUTRTL style should work. :)

                                            Can SetWindowLongPtr() be used via 'ctypes`?

                                            Thanks again and good night.

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