在 VBA 中如何将键盘快捷键恢复为默认值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12160949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:30:04  来源:igfitidea点击:

In VBA how do I restore keyboard shortcut to default?

vbakeyboard-shortcuts

提问by Josh

In my VBA Excel add-in, I allow the user to assign arbitrary keyboard shortcuts to my public macros. I allow them to change these shortcuts at runtime (really, they change a configuration file and then reload it). I use Application.OnKeyto do this.

在我的 VBA Excel 加载项中,我允许用户为我的公共宏分配任意键盘快捷键。我允许他们在运行时更改这些快捷方式(实际上,他们更改配置文件然后重新加载它)。我Application.OnKey用来做这个。

If the user changes a shortcut at runtime, how can I restore any previous default definition for the old keyboard shortcut?

如果用户在运行时更改了快捷方式,如何恢复旧键盘快捷方式的任何先前默认定义?

For example, if I have programmatically assigned ^Sto a macro, and then change it to ^E, how can I restore ^Sto the original Excel "Save" behavior?

例如,如果我以编程方式分配^S给一个宏,然后将其更改为^E,我如何才能恢复^S到原来的 Excel“保存”行为?

回答by SeanC

To return a key defined through OnKeyto its default, define it with no procedure.

要将通过定义的键返回OnKey到其默认值,请在没有过程的情况下定义它。

This sets CTRL+Sto "SpecialPrintProc"

这将CTRL+设置S为“SpecialPrintProc”

Application.OnKey "^S", "SpecialPrintProc" 

This returns CTRL+Sto its normal meaning.

这将CTRL+S恢复为其正常含义。

Application.OnKey "^S" 

This disables CTRL+S.

这将禁用CTRL+ S

Application.OnKey "^S", ""