Python / Lua Script: Detect if a modifier is pressed when running a script
-
I guess I see now but this misunderstanding shows a way to solve it.
When you get the button command id (_tbbutton.idCommand) you are able
to execute it via notepads menuCommand function likenotepad.menuCommand(_tbbutton.idCommand)
so my latest example with the logic like this
global RIGHT_CLICK_MODE if msg == 0x0201: # left RIGHT_CLICK_MODE = False ... elif msg == 0x0204: RIGHT_CLICK_MODE = True ... pressed = windll.user32.SendMessageA(self.toolbar_hwnd, 1024+2, # TB_CHECKBUTTON(WM_USER + 2) _tbbutton.idCommand, True) # execute script notepad.menuCommand(_tbbutton.idCommand) elif msg == 0x0205: RIGHT_CLICK_MODE = False pressed = windll.user32.SendMessageA(self.toolbar_hwnd, 1024+2, # TB_CHECKBUTTON(WM_USER + 2) _tbbutton.idCommand, False)
should do what you want.
Cheers
Claudia -
Hello Claudia,
That’s brilliant. Thanks again for your time.
If you want the command executed on right-up, move
RIGHT_CLICK_MODE = True # execute script notepad.menuCommand(_tbbutton.idCommand)
to
elif msg == 0x0205:
.Also, you can use
windll.user32.mouse_event(0x0002, 0, 0, 0, 0)
without a timer.def sciWndProc(self, hWnd, msg, wParam, lParam): global REAL_RIGHT_UP global RIGHT_CLICK_MODE if msg == 0x0204: # WM_RBUTTONDOWN. REAL_RIGHT_UP = False 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 REAL_RIGHT_UP: # Another WM_RBUTTONUP is sent when the right button is released. Process it now. RIGHT_CLICK_MODE = True windll.user32.mouse_event(0x0004, 0, 0, 0, 0) # Left up. REAL_RIGHT_UP = True elif REAL_RIGHT_UP and msg == 0x0201: # WM_LBUTTONDOWN. RIGHT_CLICK_MODE = False return windll.user32.CallWindowProcA (self.oldWndProc, hWnd, msg, wParam, lParam)
Have a nice weekend.
-
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 shortermouse_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.
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/8fed28f8ffe494c5e68a02010fddcdac4b4d3f74Do 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.
-
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 -
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 -
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