Extending the native GamepadAPI? - html

I'm trying to extend the native GamepadAPI to include custom controller code.
Using TypeScript, I implemented a simple function, to dispatch a "gamepadconnected" event.
// simulate gamepadconnected event
function dispatchGamepadConnectedEvent() {
let gamepad = Object.create(Gamepad.prototype);
console.log(gamepad);
let event = new GamepadEvent('gamepadconnected', {
gamepad: gamepad
})
window.dispatchEvent(event);
console.log('Gamepad connect event dispatched.');
}
However, when dispatching the event, I get an error:
Gamepad {}axes: (...)buttons: (...)connected: (...)id: (...)index: (...)mapping: (...)timestamp: (...)vibrationActuator: (...)__proto__: Gamepad
extension.ts:37 Uncaught TypeError: Failed to construct 'GamepadEvent': member gamepad is not of type Gamepad.
at dispatchGamepadConnectedEvent (extension.ts:37)
at extension.ts:48
Even though, the instantiated Gamepad object seems fine, the type of the Gamepad is not correct.
Why is this like that? How can I create a new, proper Gamepad object to fire the native event?

You cannot create a native Gamepad object.
Object.create(Gamepad.prototype) does not create a Gamepad. It creates an object and sets it prototype to Gamepad.prototype. While this trickery often works for application-level APIs, it won't fool native browser APIs.
Your choices are:
Pass null: {gamepad: null}
Pass an actual gamepad reference {gamepad: navigator.getGamepads()[0]}
Dispatch a CustomEvent, not a GamepadEvent.
Try something else entirely.

Not sure about the use case that's behind your question, but one alternative might be to use the WebHID API. I have recently used this API to create a Nintendo Joy-Con driver in JavaScript. If there happen to be reverse engineering efforts for your device in question (hint: check if there is a Linux driver), chances are that you can use this to talk to your device over WebHID.

Related

How to Simulate a Keyboard Event in Dart HTML

1. Problem and Context
I'm trying to create a browser extension with Dart, more specifically, one that deals with adding keyboard shortcuts to certain websites.
To guarantee better TDD, I'm trying to simulate keyboard events during the tests. However, it seems like many things have changed when it comes to Dart's HTML APIs and I haven't been able to figure out how to make them work.
2. What I'm Trying
I would like to register a specific keystroke — so the generic KeyboardEvent won't cut it apparently, only the KeyEvent class can handle that. So far, I'm trying something like this:
document.body.dispatchEvent(KeyEvent('keypress', keyCode: 65, charCode: 97));
From which I receive an error close to this:
Error: Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'.
I've also tried the code above with document, document.window and window, none worked. Something similar can be found in the KeyEvent documentation. However, they mention adding to a Stream, which I don't think is possible. It would have to be a Sink instead.
If I'm not able to solve this via dispatching events, I think I'll have to simply inject mocks into the tests.
3. More Resources
2 outdated answers which have helped me so far were:
dart how to create, listen, and emits custom event?
How do I listen for custom events in Dart?

How to instantiate H264 Encoder on WinRT Store App

I want to be able to encode video frames using Media Foundation IMFTransform for H264 Video Encoding. That's easily doable in Win32, where you can use MFTEnumEx to enumerate the transforms and find the H264 encoder.
However, on WinRT (Store Apps), I can't find a way to instantiate.
I've noticed there's a class CMSH264EncoderMFT, but there's no definition for the CLSID to use on CoCreateInstance.
With:
CoCreateInstance(CLSID_CMSH264EncoderMFT, nullptr, CLSCTX_INPROC_SERVER, __uuidof(IUnknown), (void **)&pUnknown);
CLSID_CMSH264EncoderMFT is undefined for WinRT apps.
And tried:
ComPtr<CMSH264EncoderMFT> encoder = Make<CMSH264EncoderMFT>();
It says the class CMSH264EncoderMFT is incomplete, and says "use of undefined type 'CMSH264EncoderMFT'". Don't even know if the syntax for Make is correct or appropriate...
Does anyone have a clue on how to do this for WinRT?
Use MFCreateSinkWriterFromURL to create a file writer first. Then, use MFCreateMediaType to create an IMFMediaType. Set up its properties, one of which will be the output format: use SetGUID method on the media type with MF_MT_SUBTYPE guid and specify MFVideoFormat_H264 as the argument. Finally, use AddStream method on the sink writer to set the media type to it.
There's an example here (you'll need to modify it a bit when it sets MF_MT_SUBTYPE).
you cannot instantiate object via CMSH264EncoderMFT because it DOES NOT have some interfaces which MUST have object in WinRT for example IInspectable - Provides functionality required for all Windows Runtime classes. CMSH264EncoderMFT IS NOT WinRT class. You can try resolve your task by function MFCreateSinkWriterFromMediaSink - this function takes an object with interface IMFMediaSink. It is possible write code for object with IMFMediaSink interface and receive samples from IMFTransform::ProcessOutput. I just point your attention - you cannot instantiate in WindowsStore code objects which IS NOT Windows Runtime classes.
Regards,
Evgeny Pereguda

Extend Function in Actionscript 3.0

Hi I am creating a Lexer, Parser, and Interpreter for my own simple scripting language in AS3. I'd like this to work similar to JavaScript in that I want to be able to execute instructions at runtime from a parsed script. There is a library for flash that does this but the issue is that VMFunctions are unable to be treated as native flash functions. I'd like to be able to call addEventListener() on native flash objects from the scripting language but I want to pass VM functions to the listener parameter which accepts only native functions. I might have to put in a work around which I know is possible but it would be more intuitive to have an object that the AVM could recognize as a native function maybe using flash_proxy and the Proxy class. So in short, I'd like to create a custom vm function class that extends the native function class in some way or can be treated as such so that when I pass the custom VM function into the addEventListener() method It will not throw a TypeReference kind of error. Thank you.
There's a short answer: Wrap it. You want to call a VMFunction when an event arises? Create an event listener for that event on the control desired, then call the function passing correct data to it. Since this is a parser, it should probably want some text, so here's an example:
var tf:TextField; // your text field, populated elsewhere
var parseButton:Sprite; // click this to parse, bla blabla
function onClick(e:MouseEvent):void {
yourVMFunction(tf.text); // parse output if necessary
}
function Parser() { // constructor
// some code not depicted
parseButton.addEventListener(MouseEvent.CLICK,onClick);
}
The yourVMFunction can be a variable of type Function, should you need to repopulate the code.

In WinRT MVVM Using Prism how do I listen for a SetProperty notification?

I am using Prism for Windows Runtime to help with my MVVM implementation. It has a built in SetProperty command for properties. How do I listen for a property change from within the View Model to make something else happen?
You can handle INotifyPropertyChanged.PropertyChanged event and check the name of the property. If you want to avoid string comparisons - you can define a new event and raise it for specific property. It can either be a regular CLR event or an EventAggregator one if you want to broadcast it to any interested listeners in the app.

Firing a KeyDown event in WinRT

Is there a way to fire a custom KeyUp/KeyDown event on the CoreWindow?
For example, take the following event:http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.core.corewindow.keyup.aspx.
My application already uses CoreWindow::KeyUp and CoreWindow::KeyDown to handle events. I am trying to test that the correct delegates are being attached and thus called when an event happens.
Note that I can't call the delegate function directly since it will not test the fact that the delegate is attached to the event.
I am looking for an answer similar to https://stackoverflow.com/a/3977396/756356.
I doubt that's possible because it sounds like something possibly interfering with the sandbox concept of modern apps. You could maybe insert some layer between the CoreWindow and your handlers and bubble the events through that layer to make is possible to raise your proxy events. I would recommend against that though since that just adds code you don't need.