简体   繁体   English

是否可以使用键盘来触发JavaScript中的文件浏览器?

[英]Is it possible to use the keyboard to trigger a File Browser in JavaScript?

I have code that let my users open the File Browser of the client's browser so they can select a file. 我有让我的用户打开客户端浏览器的文件浏览器的代码,以便他们可以选择文件。

That works fine when the user clicks a button with the mouse, but somehow it completely fails with the keyboard. 当用户用鼠标单击一个按钮时,这种方法可以很好地工作,但是由于某种原因,它在键盘上完全无法使用。

So I setup the button as follow: 所以我将按钮设置如下:

var that = this,
    upload_button = jQuery(".upload-button");

upload_button.click(function(e)
    {
        e.preventDefault();
        e.stopPropagation();

        // simulate a click on the hidden file-input button
        that.upload(editor_widget);
    });

I setup the keyboard as follow (the upload_button will get focus first, obviously): 我将键盘设置如下(显然,upload_button将首先获得焦点):

upload_button.keypress(function(e)
    {
        if(!e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey)
        {
            switch(e.which)
            {
            case 85:   // [U]pload
            case 13:   // Enter
                e.preventDefault();
                e.stopPropagation();

                // simulate a click on the hidden file-input Browse button
                that.upload(editor_widget);
                break;

            }
        }
    });

Then the upload function looks like this: 然后,上传功能如下所示:

....prototype.upload = function(editor_widget)
{
    var upload_button = jQuery(".upload-button"),
        upload_input_file = w.find(".file-input input");

    // ignore clicks if the button does not exist
    if(!upload_button.exists())
    {
        return;
    }

    // simulate a click on the file "Browse" button
    //
    upload_input_file.focus();
    upload_input_file.click();
    c.focus();
};

So, somehow the upload_input_file.click(); 因此,不知何故upload_input_file.click(); works fine when I click the button. 单击按钮时,效果很好。 It completely fails when I press U or <enter> ... 当我按U<enter>时,它完全失败...

I'm primarily testing in Firefox at the moment. 我目前主要在Firefox中进行测试。

You can totally do this. 您完全可以做到这一点。 Register keyup event for document then trigger click to file browser button. 注册文档的keyup事件,然后触发单击以单击文件浏览器按钮。

$(document).keyup(function (e) {
  if (e.keyCode === 85) {
     $(".upload-button").click(); 
  }
});  

Try substituting using .click() on DOM element for jQuery .click() on jQuery object 尝试使用DOM元素上的.click()替换jQuery上的.click()

 $(window).on("keydown", function(event) { if (event.which === 13 || event.which === 85) { event.preventDefault(); event.stopImmediatePropagation(); $("input")[0].click() } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <input type="file" /> 

As mentioned in the comments of the other two answers, I found something that will make the Enter (and eventually spacebar) work in Firefox, but not the U key. 正如在其他两个答案的评论中所提到的,我发现可以使Enter(并最终成为空格键)在Firefox中工作的东西,而不是U键。

The idea is to change the focus to the input of type file in keydown and thus let the normal Enter or Space keyup event do what it normally does. 想法是将焦点更改为keydown中类型文件的输入,从而让普通的Enter或Space键输入事件执行其通常的操作。

upload_button.keydown(function(e)
{
    if(!e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey)
    {
        switch(e.which)
        {
        case 85:   // [U]pload -- does not work in Firefox
        case 13:   // Enter
        case 32:   // Space
            // we cannot simulate a click in this case, but we can
            // change the focus and let the system manage the keyup
            // event as normal... be sure to let the normal propagation
            // of the event happen since that's where things
            // actually happens. So all we do is:
            upload_input_file.focus();
            break;

        }
    }
});

I have not tested yet, I may have to manage the U key with the click() so it works as expected in other browsers. 我尚未测试,可能必须使用click()管理U键,以便它在其他浏览器中可以正常工作。


Okay, I now also tested with IE and it works with the U key with the original code. 好的,我现在也使用IE进行了测试,它可以与带有原始代码的U键一起使用。 The Space and Enter keys do not both work in IE so if you want to support both, that will require a test to know whether to handle those keys as in IE or as in Firefox... 空格键和Enter键在IE中都不起作用,因此,如果您要同时支持这两个键,则需要进行测试以了解是否要像IE或Firefox中那样处理这些键...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM