LoginSignup
11
12

More than 5 years have passed since last update.

Node.jsでWindowsのキー状態を取得する

Posted at

document.addEventListener('keydown')のようにアプリケーションにフォーカスがある時にキー入力を検知するのではなく、バックグラウンドで起動している状態でOSのキー入力状態を取得します。いわゆるキーロガーみたいなことができます。

var FFI = require('ffi');

var user32 = new FFI.Library('user32', {
  'GetAsyncKeyState': [
    'int32', ['int32']
  ]
});

setInterval(function() {

  for (var i = 0; i < 255; ++i) {
    var state = user32.GetAsyncKeyState(i);
    if ( state == -32767) {
      // i = 押されたキーコード
      console.log(i);
    }
  }

}, 10);
11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12