dail8859 / LuaScript

Notepad++ plugin for Lua scripting capabilities
https://dail8859.github.io/LuaScript/
GNU General Public License v2.0
109 stars 23 forks source link

'Editor:ClearCmdKey(keycode, modifiers)' doesn't clear key #102

Closed TetraTheta closed 1 year ago

TetraTheta commented 1 year ago

I'm trying to disable Scintilla keyboard shortcut which cannot be disabled by Notepad++ via LuaScript. I've saw a comment that LuaScript can do it.

Original comment was saying that this Lua code will work.

editor:AssignCmdKey(string.byte('E'), SCMOD_CTRL, SCI_NULL)
editor:AssignCmdKey(string.byte('R'), SCMOD_CTRL, SCI_NULL)
editor:AssignCmdKey(string.byte('E'), SCMOD_CTRL + SCMOD_SHIFT, SCI_NULL)
editor:AssignCmdKey(string.byte('Y'), SCMOD_CTRL + SCMOD_SHIFT, SCI_NULL)
editor:AssignCmdKey(string.byte('W'), SCMOD_CTRL + SCMOD_SHIFT, SCI_NULL)
editor:AssignCmdKey(string.byte('O'), SCMOD_CTRL + SCMOD_SHIFT, SCI_NULL)
editor:AssignCmdKey(string.byte('A'), SCMOD_CTRL + SCMOD_SHIFT, SCI_NULL)
editor:AssignCmdKey(string.byte('D'), SCMOD_CTRL + SCMOD_SHIFT, SCI_NULL)
editor:AssignCmdKey(string.byte('G'), SCMOD_CTRL + SCMOD_SHIFT, SCI_NULL)
editor:AssignCmdKey(string.byte('H'), SCMOD_CTRL + SCMOD_SHIFT, SCI_NULL)
editor:AssignCmdKey(string.byte('Z'), SCMOD_CTRL + SCMOD_SHIFT, SCI_NULL)
editor:AssignCmdKey(string.byte('X'), SCMOD_CTRL + SCMOD_SHIFT, SCI_NULL)
editor:AssignCmdKey(string.byte('C'), SCMOD_CTRL + SCMOD_SHIFT, SCI_NULL)
editor:AssignCmdKey(string.byte('V'), SCMOD_CTRL + SCMOD_SHIFT, SCI_NULL)
editor:AssignCmdKey(string.byte('B'), SCMOD_CTRL + SCMOD_SHIFT, SCI_NULL)
editor:AssignCmdKey(string.byte('N'), SCMOD_CTRL + SCMOD_SHIFT, SCI_NULL)
editor:AssignCmdKey(string.byte('6'), SCMOD_CTRL + SCMOD_SHIFT, SCI_NULL)

But after searching official documentation, I found that there is Editor:ClearCmdKey for clearing keyboard shortcut, which is exactly what I want.

So I modified startup.lua script to be like this:

editor:ClearCmdKey(string.byte('E'), SCMOD_CTRL)
editor:ClearCmdKey(string.byte('R'), SCMOD_CTRL)
editor:ClearCmdKey(string.byte('E'), SCMOD_CTRL + SCMOD_SHIFT)
editor:ClearCmdKey(string.byte('Y'), SCMOD_CTRL + SCMOD_SHIFT)
editor:ClearCmdKey(string.byte('W'), SCMOD_CTRL + SCMOD_SHIFT)
editor:ClearCmdKey(string.byte('O'), SCMOD_CTRL + SCMOD_SHIFT)
editor:ClearCmdKey(string.byte('A'), SCMOD_CTRL + SCMOD_SHIFT)
editor:ClearCmdKey(string.byte('D'), SCMOD_CTRL + SCMOD_SHIFT)
editor:ClearCmdKey(string.byte('G'), SCMOD_CTRL + SCMOD_SHIFT)
editor:ClearCmdKey(string.byte('H'), SCMOD_CTRL + SCMOD_SHIFT)
editor:ClearCmdKey(string.byte('Z'), SCMOD_CTRL + SCMOD_SHIFT)
editor:ClearCmdKey(string.byte('X'), SCMOD_CTRL + SCMOD_SHIFT)
editor:ClearCmdKey(string.byte('C'), SCMOD_CTRL + SCMOD_SHIFT)
editor:ClearCmdKey(string.byte('V'), SCMOD_CTRL + SCMOD_SHIFT)
editor:ClearCmdKey(string.byte('B'), SCMOD_CTRL + SCMOD_SHIFT)
editor:ClearCmdKey(string.byte('N'), SCMOD_CTRL + SCMOD_SHIFT)
editor:ClearCmdKey(string.byte('6'), SCMOD_CTRL + SCMOD_SHIFT)
print("Hello World")

I put print("Hello World") to test if startup.lua is actually loaded or not. It loads up because I can see console output to be like this:

Hello World
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio

But when I close all Notepad++ window and start new Notepad++ and press Ctrl + Shift + D, it puts EOT character which LuaScript should prevent.

I tested editor:AssignCmdKey() too, but it does nothing neither.


Here are my debug information of Notepad++

Notepad++ v8.5.7   (64-bit)
Build time : Sep  6 2023 - 23:13:49
Path : C:\Program Files\Notepad++\notepad++.exe
Command Line : 
Admin mode : OFF
Local Conf mode : OFF
Cloud Config : OFF
OS Name : Windows 11 Pro (64-bit)
OS Version : 22H2
OS Build : 22621.2361
Current ANSI codepage : 949
Plugins : 
    LuaScript (0.12)
    mimeTools (2.9)
    NppConverter (4.5)
    NppEditorConfig (0.4)
    NppExport (0.4)
    NppSaveAsAdmin (1.0.211)
    XMLTools (3.1.1.13)
    zoomdisabler_x64 (1.2)
dail8859 commented 1 year ago

Hi @TetraTheta

Notepad++ does some not-so-obvious things when starting up. In this case what is happening is the LuaScript plugin is running the script (and removing the shortcut keys successfully) before Notepad++ applies all of its settings, which is undoing all the changes from the script.

You can use the OnReady callback to wait until Notepad++ is fully initialized.

The other thing to keep in mind is editor is a reference to the currently active editor, but there are two concrete implementations editor1 and editor2 so you will want to make sure to apply the settings to both editors.

I tested this and got it to work.


function RemoveKeys(e)
    e:ClearCmdKey(string.byte('E'), SCMOD_CTRL)
    e:ClearCmdKey(string.byte('R'), SCMOD_CTRL)
    e:ClearCmdKey(string.byte('E'), SCMOD_CTRL + SCMOD_SHIFT)
    e:ClearCmdKey(string.byte('Y'), SCMOD_CTRL + SCMOD_SHIFT)
    e:ClearCmdKey(string.byte('W'), SCMOD_CTRL + SCMOD_SHIFT)
    e:ClearCmdKey(string.byte('O'), SCMOD_CTRL + SCMOD_SHIFT)
    e:ClearCmdKey(string.byte('A'), SCMOD_CTRL + SCMOD_SHIFT)
    e:ClearCmdKey(string.byte('D'), SCMOD_CTRL + SCMOD_SHIFT)
    e:ClearCmdKey(string.byte('G'), SCMOD_CTRL + SCMOD_SHIFT)
    e:ClearCmdKey(string.byte('H'), SCMOD_CTRL + SCMOD_SHIFT)
    e:ClearCmdKey(string.byte('Z'), SCMOD_CTRL + SCMOD_SHIFT)
    e:ClearCmdKey(string.byte('X'), SCMOD_CTRL + SCMOD_SHIFT)
    e:ClearCmdKey(string.byte('C'), SCMOD_CTRL + SCMOD_SHIFT)
    e:ClearCmdKey(string.byte('V'), SCMOD_CTRL + SCMOD_SHIFT)
    e:ClearCmdKey(string.byte('B'), SCMOD_CTRL + SCMOD_SHIFT)
    e:ClearCmdKey(string.byte('N'), SCMOD_CTRL + SCMOD_SHIFT)
    e:ClearCmdKey(string.byte('6'), SCMOD_CTRL + SCMOD_SHIFT)
end

npp.AddEventHandler("OnReady", function()
    RemoveKeys(editor1)
    RemoveKeys(editor2)
    print("Hello World")
    return false
end)
``
TetraTheta commented 1 year ago

Thanks, that solved the issue!