Community
    • Login

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

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    60 Posts 5 Posters 55.0k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • YaronY
      Yaron
      last edited by

      Claudia,

      Strange - works for me.

      Do you mean the output is RTL when you open the console?

      I’ve removed my other code for testing it.

      Actually I don’t need it on startup. I can write a “Show Console” script.

      Thank you.

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

        Do you mean the output is RTL when you open the console?

        yes, on my side it is.

        When I run a second time from my test script it doesn’t change until
        another write statement happens, then it is back to LTR.

        Cheers
        Claudia

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

          Interesting.

          May you share your startup.py?

          I missed that. Sorry.

          # The lines up to and including sys.stderr should always come first
          # Then any errors that occur later get reported to the console
          # If you'd prefer to report errors to a file, you can do that instead here.
          import sys
          from Npp import *
          
          # Set the stderr to the normal console as early as possible, in case of early errors
          sys.stderr = console
          
          # Define a class for writing to the console in red
          class ConsoleError:
          	def __init__(self):
          		global console
          		self._console = console;
          
          	def write(self, text):
          		self._console.writeError(text);
          
          # Set the stderr to write errors in red
          sys.stderr = ConsoleError()
          
          # This imports the "normal" functions, including "help"
          import site
          
          # This sets the stdout to be the currently active document, so print "hello world",
          # will insert "hello world" at the current cursor position of the current document
          sys.stdout = editor
          
          ##################################################
          
          
          from ctypes import windll, byref, wintypes, WINFUNCTYPE, Structure, sizeof
          
          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):
              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')
          

          Thank you.

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

            When I run a second time from my test script it doesn’t change until
            another write statement happens, then it is back to LTR.

            On my machine it changes immediately. It’s gibberish until writing again.

            Thanks.

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

              Yaron,

              strange - might be linux/windows related.
              I’ve copied your startup.py - cleaned mine (commented everything)
              and now I have your behavior. Phew …

              Yaron, it is already half past 4 here and I have to go to late shift today.
              Need to have some sleep ;-) I will follow up on this in ~16 hours.
              Maybe earlier if we don’t have to do much at work and I can have look.

              Good night.
              Cheers
              Claudia

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

                Claudia,

                Thank you very much. You are great!

                default

                Sorry for keeping you up so late.
                Sleep well and have a nice day. :)

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

                  Thank you :-D

                  Did some last test - looks like it a
                  console.show()
                  before the code
                  and if you wish
                  console.hide()
                  afterwards does the trick.

                  Good night.

                  Cheers
                  Claudia

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

                    Very nice.

                    Thanks again. Highly appreciated.

                    Good night. :)

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

                      Yaron,

                      did try to understand what the difference could be but …
                      While investigating the python script code I assume I found what needs to be changed in
                      order to force a LTR layout.

                      The file StaticDialog.cpp has a function makeRTLResource.
                      Within the function there is this if condition

                      if (pMyDlgTemplateEx->signature == 0xFFFF)
                      	pMyDlgTemplateEx->exStyle |= WS_EX_LAYOUTRTL;
                      else
                      	(*ppMyDlgTemplate)->dwExtendedStyle |= WS_EX_LAYOUTRTL;
                      

                      what about giving a try to use

                      if (pMyDlgTemplateEx->signature == 0xFFFF)
                      	pMyDlgTemplateEx->exStyle;
                      else
                      	(*ppMyDlgTemplate)->dwExtendedStyle;
                      

                      Yaron, sorry, I would love to see why our machines behave differently but without a debugging environment it’s hard to track down the issue maybe this little code change is a better alternative.

                      Cheers
                      Claudia

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

                        Hello Claudia,

                        did try to understand what the difference could be but …
                        …
                        I would love to see why our machines behave differently but without a debugging environment it’s hard to track down the issue…

                        I apologize for not being clear enough in my last post.
                        In “Very nice” I meant to confirm the issue was solved by adding console.show(). :)

                        The file StaticDialog.cpp has a function makeRTLResource.
                        Within the function there is this if condition

                        Thank you very much for that too.
                        A couple of years ago Dave uploaded a PythonScript v1.1.1 without committing the changes.
                        As your script works perfectly, I’d rather keep using v1.1.1.

                        Many thanks again for your kind help. I do appreciate it.

                        Best regards.

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