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.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();
works fine when I click the button. It completely fails when I press U
or <enter>
...
I'm primarily testing in Firefox at the moment.