VueJS通过已安装的事件侦听器侦听键组合

时间:2018-08-23 10:29:24

标签: javascript vue.js vuejs2 frontend

我有正在使用的全局按键侦听器,但是它只能捕获单个按键。如何捕捉ctrl + enter之类的组合?

mounted() 
{
    window.addEventListener
    (
        "keypress", e => 
        {
          console.log(e.key);
        }
    );
},

Input device events

click, contextmenu, DOMMouseScroll, dblclick, gamepadconnected, gamepaddisconnected, keydown, keypress, keyup, MozGamepadButtonDown, MozGamepadButtonUp, mousedown, mouseenter, mouseleave, mousemove, mouseout, mouseover, mouseup, mousewheel, MozMousePixelScroll, pointerlockchange, pointerlockerror,wheel

似乎没有一个适合它

也无法正常工作

"keypress", e => 
{
    if (e.ctrlKey)
    {
        console.log(e.key);
    }
}

1 个答案:

答案 0 :(得分:2)

尝试一下,我们正在检查是否按下了ctrl,而(另一个)按下的键不是ctrl:

@api_0.route('/_calculate_pf_status/<task_id>', methods=['GET'])
@login_required
def _calculate_pf_status(task_id):
    task = portfolio_loader.AsyncResult(task_id)
    messages = {
        'PENDING': 'Preparing calculation agent..',
        'STARTED': 'Calculation is in progress..',
        'SUCCESS': 'Calculation has finished. Loading..',
        'FAILURE': 'The calculation has failed, please retry, check your data or contact support..'
    }
    response = {
        'state': task.state,
    }
    if task.state not in messages.keys():
        response.update({
            'status': 'An unknown error has occured, please retry, check your data or contact support..' + '\r\n'
                      + str(task.info)
        })
    else:
        response.update({
            'status': messages[task.state]
        })
    return jsonify(response)

JSFiddle

您可能还对使用vue-global-events感兴趣,它允许您以Vue风格(例如window.addEventListener ( "keydown", e => { var evt = window.event ? event : e; if (evt.ctrlKey && evt.keyCode !== 17) { console.log('Ctrl + ' + e.key); } } ); )编写全局键侦听器。