Python / Lua Script: Detect if a modifier is pressed when running a script
-
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 -
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.
-
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.
-
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 -
Claudia,
Thank you very much. You are great!
Sorry for keeping you up so late.
Sleep well and have a nice day. :)