• Login
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.1k 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.
  • C
    Claudia Frank @Yaron
    last edited by Claudia Frank Mar 31, 2017, 1:57 PM Mar 31, 2017, 1:56 PM

    @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
    • Y
      Yaron
      last edited by Mar 31, 2017, 11:37 PM

      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
      • Y
        Yaron
        last edited by Apr 1, 2017, 8:59 PM

        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.

        C 2 Replies Last reply Apr 2, 2017, 2:57 PM Reply Quote 0
        • C
          Claudia Frank @Yaron
          last edited by Apr 2, 2017, 2:57 PM

          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
          • C
            Claudia Frank @Yaron
            last edited by Claudia Frank Apr 2, 2017, 10:35 PM Apr 2, 2017, 10:35 PM

            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
            • C
              Claudia Frank
              last edited by Apr 2, 2017, 10:49 PM

              I’m talking about something like this

              Cheers
              Claudia

              1 Reply Last reply Reply Quote 0
              • Y
                Yaron
                last edited by Apr 3, 2017, 12:21 AM

                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
                • C
                  Claudia Frank
                  last edited by Claudia Frank Apr 3, 2017, 12:43 AM Apr 3, 2017, 12:42 AM

                  Hello Yaron,

                  ok and yes SetWindowLongPtr() can be used.
                  I didn’t came across a windows api function which can’t be used with ctypes yet.
                  This is a toggle function which sets WS_EX_LAYOUTRTL if not set and unset
                  if was already set.

                  WNDENUMPROC = WINFUNCTYPE(wintypes.BOOL,
                                            wintypes.HWND,
                                            wintypes.LPARAM)
                  
                  
                  python_script_hwnd = None
                  python_script_sci_handle = None
                  
                  GWL_EXSTYLE = -20
                  WS_EX_LAYOUTRTL = 0x00400000
                  
                  def EnumCallback(hwnd, lparam):
                      global python_script_hwnd
                      global python_script_sci_handle
                      
                      curr_class = (wintypes.WCHAR * 256)()
                      curr_name = (wintypes.WCHAR * 256)()
                     
                      windll.user32.GetClassNameW(hwnd, curr_class, 256)
                      windll.user32.GetWindowTextW(hwnd, curr_name, 256)
                  
                      if curr_name.value.lower() == 'python script':
                          python_script_hwnd = hwnd
                      else:
                          if python_script_hwnd is not None:
                              if curr_class.value.lower() == 'scintilla':
                                  if windll.user32.GetParent(hwnd) == python_script_hwnd:
                                      python_script_sci_handle = hwnd
                                      return False
                         
                      return True
                  
                  parent = windll.user32.FindWindowA('Notepad++', None)
                  windll.user32.EnumChildWindows(parent, WNDENUMPROC(EnumCallback), 0)
                  windll.user32.EnumChildWindows(python_script_hwnd, WNDENUMPROC(EnumCallback), 0)
                  
                  exStyle = windll.user32.GetWindowLongA(python_script_sci_handle, GWL_EXSTYLE)
                  
                  if (exStyle & WS_EX_LAYOUTRTL):
                      exStyle = exStyle & WS_EX_LAYOUTRTL
                  else:
                      exStyle = exStyle | WS_EX_LAYOUTRTL
                  windll.user32.SetWindowLongA(python_script_sci_handle, GWL_EXSTYLE, exStyle);
                  

                  You have to use SetWindowLongPtr where I use Get/SetWindowLongA as
                  SetWindowLongPtr has not been transferred to wine.

                  Cheers
                  Claudia

                  1 Reply Last reply Reply Quote 0
                  • Y
                    Yaron
                    last edited by Apr 3, 2017, 1:01 AM

                    Hello Claudia,

                    Thank you very much. This is beautiful!

                    I get the AttributeError: function 'GetWindowLongPtr' not found error.
                    What should I import from ctypes?

                    BR

                    1 Reply Last reply Reply Quote 0
                    • C
                      Claudia Frank
                      last edited by Claudia Frank Apr 3, 2017, 1:05 AM Apr 3, 2017, 1:04 AM

                      Hi Yaron,
                      you have to use either the ansi or unicode version

                      Unicode and ANSI names GetWindowLongPtrW (Unicode) and GetWindowLongPtrA (ANSI)

                      Same is true for Set…

                      Cheers
                      Claudia

                      1 Reply Last reply Reply Quote 0
                      • Y
                        Yaron
                        last edited by Apr 3, 2017, 1:09 AM

                        Hello again,

                        I get the same error.
                        No errors with Get/SetWindowLongA but the script doesn’t work. :)

                        Thank you.

                        1 Reply Last reply Reply Quote 0
                        • C
                          Claudia Frank
                          last edited by Apr 3, 2017, 1:12 AM

                          Yaron

                          can you do an test output to the console (preferred last line of code) and run the script at least two times. Let me check about the Set/GetWindowLongPtrW.

                          Cheers
                          Claudia

                          1 Reply Last reply Reply Quote 0
                          • Y
                            Yaron
                            last edited by Apr 3, 2017, 1:23 AM

                            Claudia,

                            I had put the code in startup.py . I’ve now moved it to a separate script and run it twice.

                            default

                            can you do an test output to the console (preferred last line of code)

                            I’m not sure what you mean.

                            Aren’t you tired? Should we continue tomorrow? :)

                            Thank you.

                            C 1 Reply Last reply Apr 3, 2017, 1:27 AM Reply Quote 0
                            • C
                              Claudia Frank @Yaron
                              last edited by Claudia Frank Apr 3, 2017, 1:28 AM Apr 3, 2017, 1:27 AM

                              Yaron,
                              no I meant using the GetWindowLongA as I did.
                              Btw. it looks like 32bit Python has to use GetWindowLong whereas 64bit Python
                              uses GetWindowLongPtr.

                              So make a normal script, use my code and print something to the console at the very end of the script just to see if the output is seen. If this is the case run another time and see if something changes. If so we have to change the logic about exstlye…

                              Cheers
                              Claudia

                              1 Reply Last reply Reply Quote 0
                              • Y
                                Yaron
                                last edited by Apr 3, 2017, 1:36 AM

                                It does write to the console.

                                If so we have to change the logic about exstlye…

                                Indeed. :)

                                Thank you.

                                1 Reply Last reply Reply Quote 0
                                • Y
                                  Yaron
                                  last edited by Apr 3, 2017, 1:40 AM

                                  exStyle = exStyle & (~WS_EX_LAYOUTRTL)

                                  I’m testing it.

                                  Thank you.

                                  1 Reply Last reply Reply Quote 0
                                  • C
                                    Claudia Frank
                                    last edited by Apr 3, 2017, 1:42 AM

                                    Could you do me a favor and run the following script

                                    WNDENUMPROC = WINFUNCTYPE(wintypes.BOOL,
                                                              wintypes.HWND,
                                                              wintypes.LPARAM)
                                    
                                    
                                    python_script_hwnd = None
                                    python_script_sci_handle = None
                                    
                                    GWL_EXSTYLE = -20
                                    WS_EX_LAYOUTRTL = 0x00400000
                                    
                                    def EnumCallback(hwnd, lparam):
                                        global python_script_hwnd
                                        global python_script_sci_handle
                                        
                                        curr_class = (wintypes.WCHAR * 256)()
                                        curr_name = (wintypes.WCHAR * 256)()
                                       
                                        windll.user32.GetClassNameW(hwnd, curr_class, 256)
                                        windll.user32.GetWindowTextW(hwnd, curr_name, 256)
                                        console.write('name:{}\n'.format(curr_name.value.lower()))
                                        console.write('class:{}\n'.format(curr_class.value.lower()))
                                        if curr_name.value.lower() == 'python script':
                                            python_script_hwnd = hwnd
                                        else:
                                            if python_script_hwnd is not None:
                                                if curr_class.value.lower() == 'scintilla':
                                                    if windll.user32.GetParent(hwnd) == python_script_hwnd:
                                                        python_script_sci_handle = hwnd
                                                        return False
                                           
                                        return True
                                    
                                    parent = windll.user32.FindWindowA('Notepad++', None)
                                    console.write('parent:{}\n'.format(parent))
                                    windll.user32.EnumChildWindows(parent, WNDENUMPROC(EnumCallback), 0)
                                    console.write('{}\n'.format('-'*20))
                                    console.write('python_script_hwnd:{}\n'.format(python_script_hwnd))
                                    windll.user32.EnumChildWindows(python_script_hwnd, WNDENUMPROC(EnumCallback), 0)
                                    
                                    exStyle = windll.user32.GetWindowLongA(python_script_sci_handle, GWL_EXSTYLE) # GWL_EXSTYLE
                                    console.write('exStyle:{}\n'.format(exStyle))
                                    
                                    if (exStyle & WS_EX_LAYOUTRTL):
                                        console.write('exStyle & WS_EX_LAYOUTRTL:{}\n'.format(exStyle & WS_EX_LAYOUTRTL))
                                        exStyle = exStyle & WS_EX_LAYOUTRTL
                                    else:
                                        console.write('exStyle | WS_EX_LAYOUTRTL:{}\n'.format(exStyle | WS_EX_LAYOUTRTL))
                                        exStyle = exStyle | WS_EX_LAYOUTRTL
                                    windll.user32.SetWindowLongA(python_script_sci_handle, GWL_EXSTYLE, exStyle);
                                    console.write('test\n')
                                    

                                    and please post the output

                                    Cheers
                                    Claudia

                                    1 Reply Last reply Reply Quote 0
                                    • C
                                      Claudia Frank
                                      last edited by Apr 3, 2017, 1:43 AM

                                      Maybe XOR
                                      exStyle = exStyle ^ WS_EX_LAYOUTRTL

                                      1 Reply Last reply Reply Quote 0
                                      • Y
                                        Yaron
                                        last edited by Apr 3, 2017, 1:51 AM

                                        parent:11404816
                                        name:selected tab
                                        class:#32770
                                        name:caption
                                        class:button
                                        name:
                                        class:static
                                        name:python script
                                        class:#32770
                                        name:parent:11404816
                                        name:selected tab
                                        class:#32770
                                        name:caption
                                        class:button
                                        name:
                                        class:static
                                        name:python script
                                        class:#32770
                                        
                                        class:scintilla
                                        --------------------
                                        python_script_hwnd:4392424
                                        name:parent:11404816
                                        name:selected tab
                                        class:#32770
                                        name:caption
                                        class:button
                                        name:
                                        class:static
                                        name:python script
                                        class:#32770
                                        name:parent:11404816
                                        name:selected tab
                                        class:#32770
                                        name:caption
                                        class:button
                                        name:
                                        class:static
                                        name:python script
                                        class:#32770
                                        
                                        c
                                        class:scintilla
                                        exStyle:4194304
                                        exStyle & WS_EX_LAYOUTRTL:4194304
                                        test
                                        

                                        Both exStyle = exStyle & (~WS_EX_LAYOUTRTL) and exStyle = exStyle ^ WS_EX_LAYOUTRTL do change the the layout.

                                        I have to clear the output first. Otherwise it’s gibberish.

                                        Thank you.

                                        1 Reply Last reply Reply Quote 0
                                        • C
                                          Claudia Frank
                                          last edited by Apr 3, 2017, 1:58 AM

                                          Yaron,

                                          this seems to be my waterloo in terms of understanding bitwise operations.
                                          I always confuse when to use OR and when XOR.

                                          So, just to clarify, this works?

                                          if (exStyle & WS_EX_LAYOUTRTL):
                                              console.write('exStyle & WS_EX_LAYOUTRTL: {}\n'.format('exStyle & WS_EX_LAYOUTRTL'))
                                              exStyle = exStyle ^ WS_EX_LAYOUTRTL
                                          else:
                                              console.write('exStyle | WS_EX_LAYOUTRTL:{}\n'.format(exStyle | WS_EX_LAYOUTRTL))
                                              exStyle = exStyle | WS_EX_LAYOUTRTL
                                          windll.user32.SetWindowLongA(python_script_sci_handle, GWL_EXSTYLE, exStyle);
                                          console.write('test\n')
                                          

                                          Cheers
                                          Claudia

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