nwjs / nw.js

Call all Node.js modules directly from DOM/WebWorker and enable a new way of writing applications with all Web technologies.
https://nwjs.io
MIT License
40.36k stars 3.89k forks source link

Electron's contents.sendInputEvent equivalent #5898

Open elBarkey opened 7 years ago

elBarkey commented 7 years ago

NW: v0.22.1-sdk OS: WIN 7 SP1 64

I'm currently on scriptable browser project based on NW which is similar to NightmareJS for Electron. One of my issue is using input event to automate keyboard/mouse event. It's easy thing to do in Electron since there is a specific API available for this https://electron.atom.io/docs/api/web-contents/#contentssendinputeventevent So the input event is triggered natively Offcourse it can be done by preloading a javascript that emulate this behavior, but it would be great if NW has this feature too.

Thanks.

rogerwang commented 7 years ago

Will add this feature. Thanks.

elBarkey commented 7 years ago

@rogerwang i am kinda need this feature so much. I have a little C/C++ basics, can you or anyone please help pointing me out where to start ? so i can implement this myself or maybe do a PR.

Thanks

ghost commented 6 years ago

Hi, are there any alternatives or workarounds to do this? I really need this feature 😞

rogerwang commented 6 years ago

As far as I see this can be done with chrome.debugger API. See a sample here: https://stackoverflow.com/a/46634681/180197

ghost commented 6 years ago

Using chrome.debugger didn't help.

Here's what I tried:

   chrome.debugger.getTargets(function (targets) {
        targets.forEach(function (target, i) {
            if (target.type === 'app' || target.type === 'webview') {
                var debugee = { targetId: target.id }
                chrome.debugger.attach(debugee, '1.2', function () {
                    chrome.debugger.sendCommand(debugee, 'Input.dispatchKeyEvent', {
                        'type': 'keyDown',
                        'key': 'a',
                        'code': 'a',
                        'keyCode': 'a',
                        // 'nativeVirtualKeyCode': null,
                        // 'windowsVirtualKeyCode': null
                    }, function () {
                        chrome.debugger.sendCommand(debugee, 'Input.dispatchKeyEvent', {
                            'type': 'keyUp',
                            'key': 'a',
                            'code': 'a',
                            'keyCode': 'a',
                            // 'nativeVirtualKeyCode': null,
                            // 'windowsVirtualKeyCode': null
                        }, function () {
                            chrome.debugger.detach(debugee)
                        })
                    })
                })
            }
        })
    })

Focused input element (in webview) still doesn't recieve any keyboard keys.

rogerwang commented 6 years ago

@Christywl please verify with the sample. It could be a bug.

Christywl commented 6 years ago

Using above codes, I add a input/webview tag in index.html with nwjs-sdk-v0.29.3, don't see the keyboard keys operations.

rogerwang commented 6 years ago

@Christywl could you please upload the sample you are using?

Christywl commented 6 years ago

Please download 5898.zip. I'm not sure if my sample is correct for this feature.

ghost commented 6 years ago

How about triggering the key event when focusing on input or clicking a button and waiting 2 seconds etc. So the input is focused?