Python / Lua Script: Detect if a modifier is pressed when running a script
-
I’m talking about something like this
Cheers
Claudia -
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).
And this is NPP in RTL layout with PythonSceipt console in LTR (frame and input) and RTL (output - confusing).
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.
-
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 -
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
-
Hi Yaron,
you have to use either the ansi or unicode versionUnicode and ANSI names GetWindowLongPtrW (Unicode) and GetWindowLongPtrA (ANSI)
Same is true for Set…
Cheers
Claudia -
Hello again,
I get the same error.
No errors withGet/SetWindowLongA
but the script doesn’t work. :)Thank you.
-
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 -
Claudia,
I had put the code in startup.py. I’ve now moved it to a separate script and run it twice.
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.
-
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 -
It does write to the console.
If so we have to change the logic about exstlye…
Indeed. :)
Thank you.
-
exStyle = exStyle & (~WS_EX_LAYOUTRTL)
I’m testing it.
Thank you.
-
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 -
Maybe XOR
exStyle = exStyle ^ WS_EX_LAYOUTRTL -
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)
andexStyle = exStyle ^ WS_EX_LAYOUTRTL
do change the the layout.I have to clear the output first. Otherwise it’s gibberish.
Thank you.
-
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 -
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 -
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. :)
-
Strange - works for me.
Could it be that you already have some ctypes based code in startup.py?
Variable overwriting?
May you share your startup.py?
Or maybe comment everything except the LTR code if you have already ctypes code.Cheers
Claudia -
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.
-
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