LuaScript
LuaScript copied to clipboard
Binding "tab" key
I see here https://www.scintilla.org/ScintillaDoc.html#KeyBindings that tab is available as SCK_TAB but it can't be assigned to a shortcut npp.AddShortcut or using this:
function func1(ch)
if (ch == " ") then
print("Doesn't work (this message is never shown).");
end
print("Typed a regular character like a, b, c, d, e, etc... works.")
end
npp.AddEventHandler("OnChar", func1)
If I assign it manually in Notepad++'s settings then it works but the default action of [tab] becomes disabled (so I can't make indentation then).
Is there a way to detect [tab] so that I could insert text after it's pressed with editor:AddText("text");?
It depends exactly what you are needing to do.
At the lowest level (Scintilla) the SCK_TAB key is bound to the SCI_TAB command. I can't say for sure why it wouldn't trigger the "OnChar" event, but I would guess this is expected behavior from Scintilla. You'd probably have better luck using the "OnModification" event to see when a tab character is added, by that may not be as clean as you need.
At the Notepad++ level, "npp.AddShortcut" is used but it does not directly translate these shortcuts to Scintilla shortcuts (plus there are some limitations as to what can be assigned for some reason). If you assign a shortcut key to tab then yes it will override the Notepad++ behavior.
From the sound of it you are wanting Notepad++ to still do all of its work, and then add something extra. What I've had to do in some of my other plugins is hook the keyboard input with C code to let Notepad++ handle it like "normal" and then add some extra logic after it is done.
Thanks, it works with OnModification indeed but editor:AddText("text") doesn't work inside that callback.
Yes, I want to type for example x and [tab] and have it covert it to y (like text expander). But still retain normal [tab] functionality in all other contexts.
What I've had to do in some of my other plugins is hook the keyboard input with C code to let Notepad++ handle it like "normal" and then add some extra logic after it is done.
This could be what I'm looking for. But it sounds like it's something beyond my capabilities of implementing.
Is there a way to hook C code in Lua plugin? If so I could write that in C indeed. But I have no idea how to connect C to Lua/NPP.
I've thought about adding that functionality to this plugin to be able to hook the keyboard input from Lua which would allow users to do something before, after, block, alter, etc keypresses. Would not be useful to alot of users but could still be useful if implemented correctly.
The other option would be to compile a C module to hook the keyboard that uses the Lua library distributed with this plugin. I know it is possible but haven't done it myself in a while.