Skip to content

Instantly share code, notes, and snippets.

@lsongdev
Last active February 11, 2016 14:34
Show Gist options
  • Save lsongdev/ace194795bee627b5a61 to your computer and use it in GitHub Desktop.
Save lsongdev/ace194795bee627b5a61 to your computer and use it in GitHub Desktop.
Control Windows7/8/10 volume use IR Remote and UDP Socket
const dgram = require('dgram');
const InputEvent = require('input-event');
var remote = new InputEvent.Keyboard('/dev/input/event0');
var udp = dgram.createSocket('udp4');
remote.on('keypress', function(ev){
console.log(ev.code);
var buf = new Buffer('code:' + ev.code);
udp.send(buf, 0, buf.length, 1989, '192.168.11.117');
});
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
namespace RemoteControlServer
{
class RemoteServer
{
const int VOLUME_UP = 175;
const int VOLUME_DOWN = 174;
static void Main(string[] args)
{
var endpoint = new IPEndPoint(IPAddress.Any, 1989);
UdpClient server = new UdpClient(endpoint);
Console.WriteLine("Remote Server is running at 1989 .");
while (true)
{
var sender = new IPEndPoint(IPAddress.Any, 0);
var data = server.Receive(ref sender);
string message = Encoding.ASCII.GetString(data, 0, data.Length);
Console.WriteLine("{0}: {1}", sender, message);
switch (message)
{
case "code:7":
for(var i = 0; i < 10; i++) keybd_event((byte)VOLUME_DOWN, 0, 0, 0);
break;
case "code:9":
for (var i = 0; i < 10; i++) keybd_event((byte)VOLUME_UP, 0, 0, 0);
break;
case "code:69":
Process.Start("shutdown", "/s /t 0");
break;
}
}
}
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment