Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How are keyboard events sent to dosbox? #22

Open
napalm272 opened this issue Jul 12, 2015 · 4 comments
Open

How are keyboard events sent to dosbox? #22

napalm272 opened this issue Jul 12, 2015 · 4 comments

Comments

@napalm272
Copy link

I want to create some buttons that can be clicked on, and when these buttons are clicked, I want to send keyboard events to dosbox. I can't find a place to add this logic. I tried adding window.onkeypress. I get events before dosbox is loaded, after it's loaded I don't get any events anymore. Can you point out where I should be looking at?

Thanks

@jbanes
Copy link

jbanes commented Jul 13, 2015

It's surprisingly difficult to get browsers to trigger a keyboard event. However, after consulting various answers on the topic, the following code was what I came up with to solve the problem. See if it works for you:

function simulateKeyEvent(charCode, pressed) 
{
    var name = pressed ? "keydown" : "keyup";
    var event = document.createEvent('KeyboardEvent');
    var getter = {
        get: function() { return this.keyCodeVal; }
    };

    if(!element) return;

    // Chromium Hack
    Object.defineProperty(event, 'keyCode', getter);
    Object.defineProperty(event, 'which', getter);
    Object.defineProperty(event, 'charCode', getter);   

    if(event.initKeyboardEvent) 
    {
        event.initKeyboardEvent(name, true, true, document.defaultView, false, false, false, false, charCode, charCode);
    } 
    else 
    {
        event.initKeyEvent(name, true, true, document.defaultView, false, false, false, false, charCode, 0);
    }

    event.keyCodeVal = charCode;

    if(event.keyCode !== charCode) 
    {
        alert("keyCode mismatch " + event.keyCode + "(" + event.which + ")");
    }

    element.dispatchEvent(event);
}

@napalm272
Copy link
Author

Thanks for the response! What is element in this case? I tried using document and it didn't work.

@jbanes
Copy link

jbanes commented Jul 14, 2015

Ah, good question! Here's the code I have that sets element:

element = document.getElementById('canvas')

Basically, it should be a reference to the element that DOSBox is rendering into. The code dispatches events onto that element so that they can be captured by DOSBox.

@napalm272
Copy link
Author

Actually just using document also works! The trick is to send a keyup event shortly after a keydown event to simulate a keypress. Sending a keypress event directly doesn't work for some reason. Just sending keydown event also doesn't have the correct behavior, but I think I can work around that. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants