Skip to content

Instantly share code, notes, and snippets.

@usagi
Created April 13, 2014 04:14
Show Gist options
  • Select an option

  • Save usagi/10568899 to your computer and use it in GitHub Desktop.

Select an option

Save usagi/10568899 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <queue>
#include <emscripten/emscripten.h>
#include <emscripten/bind.h>
namespace
{
std::queue<unsigned int> keydown_buffer;
auto cxx_keydown(unsigned int key_code)
-> void
{
EM_ASM({ console.log("cxx_keydown") });
keydown_buffer.push(key_code);
}
EMSCRIPTEN_BINDINGS(my_module)
{ emscripten::function("cxx_keydown", &cxx_keydown); }
}
auto main() -> int
{
EM_ASM({ window.addEventListener("keydown", function(e){ Module.cxx_keydown(e.keyCode); }); });
emscripten_set_main_loop
( []
{
while(!keydown_buffer.empty())
{
std::cerr << keydown_buffer.front() << "\n";
keydown_buffer.pop();
}
}
, 100
, 1
);
}
@usagi

usagi commented Apr 13, 2014

Copy link
Copy Markdown
Author

compile: em++ --bind -o emscripten_keydown_sample.out.html -std=c++11 emscripten_keydown_sample.cxx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment