我最近开始使用 Electron 来创建一个免费创作音乐的应用程序。不幸的是,在尝试使用捕鼠器为其设置全局键盘快捷键时,出现了错误Uncaught TypeError: callback is not a function
。当我在 Electron 上使用开发人员工具时,它表明问题与捕鼠器源代码有关,因此我不能100%确定错误在哪里,但是,我知道该错误仅在其中一个捕鼠器中出现按键绑定(bind)已激活。以下是我的代码:
const { dialog } = require('electron').remote
const ipc = require('electron').ipcRenderer
const Vex = require('vexflow')
const Mousetrap = require('mousetrap');
//Global Variables
var keylist = {'z': 'a/4', 'x': 'b/4', 'c': 'c/4', 'v': 'd/4', 'b': 'e/4', 'n': 'f/4', 'm': 'g/4', 'a': 'a/5', 's': 'b/5', 'd': 'c/5', 'f': 'd/5', 'g': 'e/5', 'h': 'f/5', 'j': 'g/5', 'q': 'a/6', 'w': 'b/6', 'e': 'c/6', "r": 'd/6', 't': 'e/6', 'y': 'f/6', 'u': 'g/6'}
VF = Vex.Flow;
var canvas = document.getElementById("myCanvas");
var renderer = new Vex.Flow.Renderer(canvas, Vex.Flow.Renderer.Backends.CANVAS);
// Use the renderer to give the dimensions to the SVG
// Expose the context of the renderer
var context = renderer.getContext();
// And give some style to our SVG
context.setFont("Arial", 10, "").setBackgroundFillStyle("#eed");
/**
* Creating a new stave
*/
// Create a stave of width 400 at position x10, y40 on the SVG.
var stave = new VF.Stave(10, 40, 400);
// Add a clef and time signature.
stave.addClef("treble").addTimeSignature("4/4");
// Set the context of the stave our previous exposed context and execute the method draw !
stave.setContext(context).draw();
var chord = []
function newnote (e) {
console.log('triggered')
var code = (e.keyCode ? e.keyCode : e.which);
//checks to see if the key is a shortcut, and isnt enter/return
if (e in keylist && code !== 13) {
let note = keylist[e]
chord.push(note)
}
//if the key is enter, then the chord is put no the staff and chord is cleared
if (code == 13) {
stave.VF.Formatter.FormatAndDraw(context, stave, notes);
chord = []
}
else { }
}
Mousetrap.bind('x', newnote('x', stave, chord))
//etc.
感谢您的任何帮助
最佳答案
我不确定这些参数在这里代表什么,但是我认为应该是这样的:
Mousetrap.bind('x', newnote);
第二个参数应该是一个函数,但是上面得到它的方式是该函数调用的结果,因此您会遇到该错误。