Google Apps 脚本 HTML - 如何从整个文档而不只是一个按钮触发键盘事件?
Google Apps Script HTML - How to make a keyboard event trigger from an entire document rather than just a button?
对于 Google 文档的 Google Apps 脚本,在 Sidebar.html 文件中我使用此代码(已简化以删除不相关的部分):
<div class="sidebar branding-below">
<form>
<div class="block" id="button-bar">
<button id="some-action" class="action">Action</button>
</div>
</form>
</div>
<script>
$(function() {
$('#some-action').click(doSomething); //call the method below on button click
google.script.run.withSuccessHandler(loadPreferences)
.withFailureHandler(showError).getPreferences(); //not sure what this does
});
$(document).keydown(function(e){ //only applies to button??
//output whatever key was pressed under the button
var div = $('<div id="clickmsg" class="text">' + 'KeyPressed: ' + e.which + '</div>');
$('#button-bar').after(div);
//Detect CTRL + q keydown combo, which should be unused in windows and chrome
if(e.ctrlKey && e.keyCode == 81){
var div = $('<div id="clickmsg" class="text">' + 'Ctrl+Q detected' + '</div>');
$('#button-bar').after(div);
doSomething(); //calls the method below
}
});
function doSomething() {
this.disabled = true;
$('#error').remove();
google.script.run
[some code...] //doStuff
}
</script>
doSomething() 仅在我按下按钮时触发。因此,只要按钮仍在选项卡式选择下,我按下的键就会显示在其下方。但是,在我没有点击按钮的任何情况下,无论按下什么键,这个按键代码都不会输出任何东西。
请注意,我使用 $(document).keydown,但从未在任何地方定义 "document"。但是,它会以某种方式自动绑定到侧边栏中的按钮。为什么会发生这种情况,而不是返回错误?如何让它检测所有 google 文档中的键盘快捷键,即使用户只是在输入? HTML 是检测这个的正确方法吗?
我知道这(可能)是可能的,因为这个 post 关于我使用来自 issue here 的键绑定代码的问题,post代码说"the new IFRAME mode in HtmlService does allow for key codes to be passed on to Add-ons",只要用户有"click[ed] on / activate[d] the sidebar first."
您的 HTML 被 HTML 服务包装,如下所示:
<iframe>
<!DOCTYPE html>
<more boilerplate...>
...
your html
...
</iframe>
所以,document
被定义了,并且有一个范围限制在iframe
。由于表单中只有一个元素,因此焦点必须位于 document
才能接收事件。
如果您的目的是在 Google 文档中捕获所有击键,您将无法在 Google Apps 脚本中执行此操作。不过,浏览器扩展也许可以。
对于 Google 文档的 Google Apps 脚本,在 Sidebar.html 文件中我使用此代码(已简化以删除不相关的部分):
<div class="sidebar branding-below">
<form>
<div class="block" id="button-bar">
<button id="some-action" class="action">Action</button>
</div>
</form>
</div>
<script>
$(function() {
$('#some-action').click(doSomething); //call the method below on button click
google.script.run.withSuccessHandler(loadPreferences)
.withFailureHandler(showError).getPreferences(); //not sure what this does
});
$(document).keydown(function(e){ //only applies to button??
//output whatever key was pressed under the button
var div = $('<div id="clickmsg" class="text">' + 'KeyPressed: ' + e.which + '</div>');
$('#button-bar').after(div);
//Detect CTRL + q keydown combo, which should be unused in windows and chrome
if(e.ctrlKey && e.keyCode == 81){
var div = $('<div id="clickmsg" class="text">' + 'Ctrl+Q detected' + '</div>');
$('#button-bar').after(div);
doSomething(); //calls the method below
}
});
function doSomething() {
this.disabled = true;
$('#error').remove();
google.script.run
[some code...] //doStuff
}
</script>
doSomething() 仅在我按下按钮时触发。因此,只要按钮仍在选项卡式选择下,我按下的键就会显示在其下方。但是,在我没有点击按钮的任何情况下,无论按下什么键,这个按键代码都不会输出任何东西。
请注意,我使用 $(document).keydown,但从未在任何地方定义 "document"。但是,它会以某种方式自动绑定到侧边栏中的按钮。为什么会发生这种情况,而不是返回错误?如何让它检测所有 google 文档中的键盘快捷键,即使用户只是在输入? HTML 是检测这个的正确方法吗?
我知道这(可能)是可能的,因为这个 post 关于我使用来自 issue here 的键绑定代码的问题,post代码说"the new IFRAME mode in HtmlService does allow for key codes to be passed on to Add-ons",只要用户有"click[ed] on / activate[d] the sidebar first."
您的 HTML 被 HTML 服务包装,如下所示:
<iframe>
<!DOCTYPE html>
<more boilerplate...>
...
your html
...
</iframe>
所以,document
被定义了,并且有一个范围限制在iframe
。由于表单中只有一个元素,因此焦点必须位于 document
才能接收事件。
如果您的目的是在 Google 文档中捕获所有击键,您将无法在 Google Apps 脚本中执行此操作。不过,浏览器扩展也许可以。