Community
    • 登入

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

    已排程 已置頂 已鎖定 已移動 Help wanted · · · – – – · · ·
    60 貼文 5 Posters 59.6k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • YaronY
      Yaron
      最後由 編輯

      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 條回覆 最後回覆 回覆 引用 0
      • Claudia FrankC
        Claudia Frank @Yaron
        最後由 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 條回覆 最後回覆 回覆 引用 0
        • YaronY
          Yaron
          最後由 編輯

          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 條回覆 最後回覆 回覆 引用 0
          • YaronY
            Yaron
            最後由 編輯

            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 條回覆 最後回覆 回覆 引用 0
            • Claudia FrankC
              Claudia Frank @Yaron
              最後由 編輯

              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 條回覆 最後回覆 回覆 引用 0
              • YaronY
                Yaron
                最後由 編輯

                Claudia,

                Thank you very much. You are great!

                default

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

                1 條回覆 最後回覆 回覆 引用 1
                • Claudia FrankC
                  Claudia Frank
                  最後由 編輯

                  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 條回覆 最後回覆 回覆 引用 0
                  • YaronY
                    Yaron
                    最後由 編輯

                    Very nice.

                    Thanks again. Highly appreciated.

                    Good night. :)

                    1 條回覆 最後回覆 回覆 引用 0
                    • Claudia FrankC
                      Claudia Frank
                      最後由 編輯

                      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 條回覆 最後回覆 回覆 引用 0
                      • YaronY
                        Yaron
                        最後由 編輯

                        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 條回覆 最後回覆 回覆 引用 0
                        • 第一個貼文
                          最後的貼文
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors