Python / Lua Script: Detect if a modifier is pressed when running a script
-
Hello Claudia,
Thank you for the detailed explanation. I appreciate it.
So you need to have the keyboard logger registered within startup.py
I haven’t thoroughly learned the script yet.
If I have a bool
altPressed
in startup.py, I’d need to set it to false on Alt-UP only after some timeout. Is that correct?BR
-
Yaron,
in your case, if I understand it correctly, I would useif msg in [WM_KEYDOWN, WM_SYSKEYDOWN]:
to set the boolean true and
elif msg in [WM_KEYUP,WM_SYSKEYUP]:
to set it to false
and you need to handle the cases when alt together with other modifiers is pressed.
Cheers
Claudia -
Detecting a right-click would be better (i.e. execute the script on both left/right-click and detect the button).
sorry, I missed this one.
So you do have defined a button in the toolbar, correct?
And when clicking with left mouse = normal run but when clicking with right mouse then advanced run?Cheers
Claudia -
If I have a bool altPressed in startup.py, I’d need to set it to false on Alt-UP only after some timeout. Is that correct?
Or setting it to false only in the altDependentScript.
I’ll think about it.You’ve been very helpful as always. :)
Thank you very much.BR
-
Yaron, in case you want to get the mouse clicks see here.
Cheers and good night
Claudia -
Hello again Claudia,
Detecting a right-click would be better (i.e. execute the script on both left/right-click and detect the button).
…
sorry, I missed this one.
So you do have defined a button in the toolbar, correct?
And when clicking with left mouse = normal run but when clicking with right mouse then advanced run?That’s correct.
in case you want to get the mouse clicks see here.
Getting the specific command/button position would be too much. :)
Thanks again. Highly appreciated.
Good night.
-
As far as LuaScript goes you’d probably be alot more limited as you have to use what Scintilla/Notepad++ provides which you can catch double clicks and get the modifier keys but don’t think there’s a way to distinguish between left or right click. You could also register the same function with more than one keyboard shortcut, such as “F5”, “Ctrl+F5”, “Alt+F5”, etc…but this is not ideal.
Python gives more possibilities as far as interacting with the OS so this is probably your better option depending what you need exactly.
-
Hello @dail,
Thank you for the additional info. I appreciate it.
If I understand it correctly, SCN_DOUBLECLICK is only relevant to the editor.
Double-click could have been useful, but I need it for a command or a toolbar button.And thanks again for the great Lua Script plugin.
BTW, Ctrl+left-click to open the file instead of running the script could be a nice addition to Lua Script.
What do you think?BR
-
but I need it for a command or a toolbar button.
Oh! Now I see what you are doing. In this case PythonScript is your best option.
Ctrl+left-click to open the file instead of running the script could be a nice addition to Lua Script.
Honestly I don’t even know if would be possible. Since individual functions are registered instead of files, there is nothing that associates where a function is defined.
-
Hello @dail,
Oh! Now I see what you are doing. In this case PythonScript is your best option.
A simple example for what I meant:
A scriptTypeLetter
types a letter in the editor. Using Customize Toolbar, I’ve assigned a toolbar button to the script.
I want it to normally typeA
but if Alt is down when pressing the toolbar button typeB
.Claudia’s suggestion is a good option, but I think I’d have to use some timeout.
Using another scriptToggle TypeLetter Mode
is another option which should work well in Lua Script too.Since individual functions are registered instead of files…
I didn’t think about that. :)
Thanks again.
BR -
Hello Yaron
a quick script about what I meant with mouse click capture.
In one script you intercept the window proc (needs to be called first, startup.py might be good choice)
from ctypes import windll, byref, wintypes, WINFUNCTYPE, Structure, sizeof WndProcType = WINFUNCTYPE(wintypes.LONG, wintypes.HWND, wintypes.UINT, wintypes.WPARAM, wintypes.LPARAM) GWL_WNDPROC = -4 WNDENUMPROC = WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM) _g = globals() CAPTURE_MOUSE_CLICK = _g.get('CAPTURE_MOUSE_CLICK', False) OLD_WND_PROC = _g.get('OLD_WND_PROC', None) LEFT_MOUSE_CLICK_MOD = False class Hook(): def EnumCallback(self, hwnd, lparam): curr_class = (wintypes.WCHAR * 256)() windll.user32.GetClassNameW(hwnd, curr_class, 256) if curr_class.value.lower() == 'toolbarwindow32': self.toolbar_handle = hwnd return False return True def __init__(self): self.toolbar_handle = None parent = windll.user32.FindWindowA('Notepad++',None) windll.user32.EnumChildWindows(parent, WNDENUMPROC(self.EnumCallback), 0) self.oldWndProc = None self.SHIFT_PRESSED = False self.CTRL_PRESSED = False self.ALT_PRESSED = False def register(self): if self.toolbar_handle: self.new_wnd_proc = WndProcType(self.sciWndProc) windll.kernel32.SetLastError(0) self.oldWndProc = windll.user32.SetWindowLongA(self.toolbar_handle, GWL_WNDPROC, self.new_wnd_proc) if self.oldWndProc: global OLD_WND_PROC OLD_WND_PROC = self.oldWndProc else: _err = 'GetLastError:{}'.format(windll.kernel32.GetLastError()) notepad.messageBox('Could not register hook:\n{}\n'.format(_err) + 'Shortcuts won\'t work', 'Register Hook Failure', 0) else: console.write('no toolbar_handle found') def unregister(self): if OLD_WND_PROC: self.oldWndProc = OLD_WND_PROC windll.kernel32.SetLastError(0) dummy = windll.user32.SetWindowLongA(self.toolbar_handle, GWL_WNDPROC, self.oldWndProc) if not dummy: _err = 'GetLastError:{}'.format(windll.kernel32.GetLastError()) notepad.messageBox('Could not unregister hook:\n{}\n'.format(_err) + 'It is recommended to save data and restart npp to prevent data loss', 'Unregister Hook Failure', 0) else: console.write('ERROR no saved window proc found') def sciWndProc(self, hWnd, msg, wParam, lParam): if msg == 0x0201: # WM_LBUTTONDOWN global LEFT_MOUSE_CLICK_MOD if wParam == 5: # 0x0001 LButton + 0x0004 Shift console.write('left mouse click while holding shift\n') LEFT_MOUSE_CLICK_MOD = True else: console.write('left mouse click\n') LEFT_MOUSE_CLICK_MOD = False return windll.user32.CallWindowProcA (self.oldWndProc, hWnd, msg, wParam, lParam) # ----------------------------------------------------------------------------- _hook = Hook() if not CAPTURE_MOUSE_CLICK: _hook.register() CAPTURE_MOUSE_CLICK = True console.write('registered') else: _hook.unregister() CAPTURE_MOUSE_CLICK = False console.write('unregistered')
in another script something like
import datetime print datetime.datetime.now() print 'LEFT_MOUSE_CLICK_MOD:{}'.format(LEFT_MOUSE_CLICK_MOD)
The first script gets the handle of the toolbar and hooks it message queue.
Once you click a button the global variable is set and the second script gets executed.Cheers
Claudia -
Hello Claudia,
Sorry for the late reply. I was not by my PC.
And thank you for the beautiful script. I appreciate your kindness.Now that you capture both the click and the modifier, there’s no need to use timeout and it works like a charm.
Two follow-up questions with your permission:
-
Customize Toolbar inserts a separator on pressing Shift and left-clicking a toolbar button.
Which module should I import in order to useGetKeyState()
and detect Alt? -
The little programming experience I have is from Firefox.
In that browser adding a right-click functionality to a toolbar button is done as follows:
- Get A specific toolbar button object.
- Add a click listener to that button and assign your own action to right-click.
I suppose this procedure would be more complex in NPP.
Actually you can replace in your script
if wParam == 5: # 0x0001 LButton + 0x0004 Shift
with
if wParam == 3: # 0x0001 LButton + 0x0002 MK_RBUTTON
and then if you hold the right button down and left-click - you can execute the script in “alternative mode”.
Another simple (and not as elegant) way to run a script in two modes:
In startup (py or lua) addtestScriptDefaultMode = True
.
Create a scriptToggle TestScript Default Mode
containing two lines:global testScriptDefaultMode testScriptDefaultMode = not testScriptDefaultMode
In
TestScript
checkif testScriptDefaultMode:
.Assign a simple shortcut (e.g. Alt+Spacebar) to
Toggle TestScript Default Mode
and use it to quickly toggle the modes…
Thanks again.
Have a nice weekend. -
-
Customize Toolbar inserts a separator on pressing Shift and left-clicking a toolbar button.
Which module should I import in order to use GetKeyState() and detect Alt?The function is also called GetKeyState and is in user32.dll.
You need to provide the virtual key code to get the state, so something likealt_state = windll.user32.GetKeyState(0x12)
but using wine, I was not able to press alt and do left click with mouse.
Would be interesting to see if this works on Windows.The little programming experience I have is from Firefox.
In that browser adding a right-click functionality to a toolbar button is done as follows:
Get A specific toolbar button object.
Add a click listener to that button and assign your own action to right-click.
I suppose this procedure would be more complex in NPP.I think so too. Out of my mind, you would need to find notepad++ window handle,
enumerate child windows to get toolbar, enumerate toolbar child windows to get
the button in question (not sure how to identify), register a function which hooks into
buttons message queue and I don’t know what happens if another program accesses toolbar
to modify (or add/delete) a button?? I’m pretty sure it can be done but I’m not convinced
it should be done.Another possible solution to run script in alternate mode would be to use a
inputbox (prompt method) ;-) (also not as elegant but most painless version) :-DCheers
Claudia -
Hello Claudia,
The function is also called GetKeyState and is in user32.dll.
Great. Thank you.
but using wine, I was not able to press alt and do left click with mouse.
Would be interesting to see if this works on Windows.alt_state = windll.user32.GetKeyState(0x12) if alt_state < 0:
Works perfectly.
Out of my mind, you would need to find notepad++ window handle…
Your confirmation that this is indeed rather complex is important.
Another possible solution to run script in alternate mode would be to use a
inputboxSo I have multiple choices now. :)
Being a “mouse-oriented” user, your script detecting if the right button was down when left-clicking would probably fit me best.Thanks again for your time and clever ideas. I do appreciate it.
BR -
@Yaron and @Claudia-Frank
Oftentimes I find myself writing a Pythonscript that either acts on text above the caret or below the caret. So I need a way to tell it which way to go. An input box would work, but it slows down one’s “workflow”. Often it is an alternate but related keypress (e.g. F10 runs functionality above the caret, Shift+F10 for below the caret) that I want to use. So what I’ve been doing is having 3 scripts, one “common” one, and two others. The two others are tied to F10 and Shift+F10, and call the common script, telling it the direction.
So I just wanna say that this thread has gotten me thinking about how to do this cleaner; haven’t done anything about it yet, but definitely thinkin’… :-D
-
Hello Scott,
It’s good to know an experienced “Pythoneer” like yourself have been coping with this issue.
Our Claudia is really clever, isn’t she? :)Please share with us your preferred solution.
Best regards.
-
@Scott-Sumner
I don’t know that this thread will be of any help for that kind of key differentiation. I do see how it might work with a toolbar button assigned to a pythonscript, though, but this seems like it isn’t what you want. -
Hello Claudia and all,
Thanks again for the script. I appreciate it.
I’ve slightly modified it so that right-clicking a toolbar button would execute its command.
Emulating the regular left-click behavior:- When the right button is down, the toolbar button is “checked”.
- The command is executed only when the right button is released.
If anyone can think of a better way to achieve that behavior, please let me know.
from ctypes import windll, byref, wintypes, WINFUNCTYPE, Structure, sizeof WndProcType = WINFUNCTYPE(wintypes.LONG, wintypes.HWND, wintypes.UINT, wintypes.WPARAM, wintypes.LPARAM) GWL_WNDPROC = -4 WNDENUMPROC = WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM) _g = globals() CAPTURE_MOUSE_CLICK = _g.get('CAPTURE_MOUSE_CLICK', False) OLD_WND_PROC = _g.get('OLD_WND_PROC', None) RIGHT_CLICK_MODE = False REAL_RIGHT_UP = False class Hook(): def EnumCallback(self, hwnd, lparam): curr_class = (wintypes.WCHAR * 256)() windll.user32.GetClassNameW(hwnd, curr_class, 256) if curr_class.value.lower() == 'toolbarwindow32': self.toolbar_handle = hwnd return False return True def __init__(self): self.toolbar_handle = None parent = windll.user32.FindWindowA('Notepad++', None) windll.user32.EnumChildWindows(parent, WNDENUMPROC(self.EnumCallback), 0) self.oldWndProc = None def register(self): if self.toolbar_handle: self.new_wnd_proc = WndProcType(self.sciWndProc) windll.kernel32.SetLastError(0) self.oldWndProc = windll.user32.SetWindowLongA(self.toolbar_handle, GWL_WNDPROC, self.new_wnd_proc) if self.oldWndProc: global OLD_WND_PROC OLD_WND_PROC = self.oldWndProc else: _err = 'GetLastError:{}'.format(windll.kernel32.GetLastError()) notepad.messageBox('Could not register hook:\n{}\n'.format(_err) + 'Right-click mode won\'t work', 'Register Hook Failure', 0) else: console.write('no toolbar_handle found') def unregister(self): if OLD_WND_PROC: self.oldWndProc = OLD_WND_PROC windll.kernel32.SetLastError(0) dummy = windll.user32.SetWindowLongA(self.toolbar_handle, GWL_WNDPROC, self.oldWndProc) if not dummy: _err = 'GetLastError:{}'.format(windll.kernel32.GetLastError()) notepad.messageBox('Could not unregister hook:\n{}\n'.format(_err) + 'It is recommended to save data and restart npp to prevent data loss', 'Unregister Hook Failure', 0) else: console.write('ERROR no saved window proc found') 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. windll.user32.SetTimer(hWnd, 1, 1000, 0); # 1 == TIMER_ID. REAL_RIGHT_UP = True elif msg == 0x0113 and wParam == 1: # WM_TIMER. RIGHT_CLICK_MODE = False windll.user32.KillTimer(hWnd, 1); return windll.user32.CallWindowProcA (self.oldWndProc, hWnd, msg, wParam, lParam) # ----------------------------------------------------------------------------- _hook = Hook() if not CAPTURE_MOUSE_CLICK: _hook.register() CAPTURE_MOUSE_CLICK = True console.write('registered') else: _hook.unregister() CAPTURE_MOUSE_CLICK = False console.write('unregistered')
Copy and paste the code in startup.py.
In a script you want to run in two modes, (i.e. left-click and right-click) addmodeRightClick = RIGHT_CLICK_MODE
and handle it accordingly.
Since a 1 Sec timer is set to revert RIGHT_CLICK_MODE to False, it’s recommended to addmodeRightClick = RIGHT_CLICK_MODE
at the beginning of the script.Best regards.
- When the right button is down, the toolbar button is “checked”.
-
so you are concerned not seeing the pressed/checked button if right clicking on it, aren’t you?
Cheers
Claudia -
here my solution, not fully error proved and just added without any structure
but I guess you get the point.You need to import another 4 types from ctypes (pointer, c_ubyte, c_int, c_ulong)
The changed part isdef sciWndProc(self, hWnd, msg, wParam, lParam): if msg == 0x0201: #0x0201: # WM_LBUTTONDOWN global LEFT_MOUSE_CLICK_MOD if wParam == 5: # 0x0001 LButton + 0x0004 Shift console.write('left mouse click while holding shift\n') LEFT_MOUSE_CLICK_MOD = True else: console.write('left mouse click\n') elif msg == 0x0204: class TBBUTTON(Structure): _fields_ = [ ('iBitmap', c_int), ('idCommand', c_int), ('fsState', c_ubyte), ('fsStyle', c_ubyte), ('bReserved', c_ubyte * 6), # 2 if win32 ('dwData', c_ulong), ('iString', c_int), ] LEFT_MOUSE_CLICK_MOD = False _point = wintypes.POINT() result = windll.user32.GetCursorPos(byref(_point)) if result: _hwnd = windll.user32.WindowFromPoint(_point) windll.user32.ScreenToClient(_hwnd,pointer(_point)) button_id = windll.user32.SendMessageA(_hwnd, 1024+69, # TB_HITTEST(WM_USER + 69) 0, pointer(_point)) _tbbutton = TBBUTTON() bool = windll.user32.SendMessageA(_hwnd, 1024+23, # TB_GETBUTTON(WM_USER + 23)) button_id, byref(_tbbutton)) if bool: pressed = windll.user32.SendMessageA(_hwnd, 1024+2, # TB_CHECKBUTTON(WM_USER + 2) _tbbutton.idCommand, True) return windll.user32.CallWindowProcA (self.oldWndProc, hWnd, msg, wParam, lParam)
Seems to work on linux, so should work on windows as well.
Cheers
Claudia