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.2k 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,

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

                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
                • YaronY
                  Yaron
                  last edited by

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

                    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
                    • YaronY
                      Yaron
                      last edited by

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

                        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
                        • YaronY
                          Yaron
                          last edited by

                          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.

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

                            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
                            • YaronY
                              Yaron
                              last edited by

                              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
                              • YaronY
                                Yaron
                                last edited by

                                exStyle = exStyle & (~WS_EX_LAYOUTRTL)

                                I’m testing it.

                                Thank you.

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

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

                                    Maybe XOR
                                    exStyle = exStyle ^ WS_EX_LAYOUTRTL

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

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

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

                                          and there seem to be a trap. The console changes it output only if something is
                                          written to the output. So the initial state will be RTL until something gets written.

                                          Cheers
                                          Claudia

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

                                            So, just to clarify, this works?

                                            Yes, it does.

                                            and there seem to be a trap. The console changes it output only if something is
                                            written to the output. So the initial state will be RTL until something gets written.

                                            If I put the code in startup.py it doesn’t work even after writing to the console.

                                            Thanks again. :)

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