Skip to content

Instantly share code, notes, and snippets.

@SackMagiclight
Created August 6, 2016 15:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SackMagiclight/34bfdd0e9c1170a17d22388f0459dc4e to your computer and use it in GitHub Desktop.
Save SackMagiclight/34bfdd0e9c1170a17d22388f0459dc4e to your computer and use it in GitHub Desktop.
using SharpDX.DirectInput;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace IIDX_SCR_FOR_LR2
{
class Program
{
static void Main(string[] args)
{
DirectInput directInput;
Joystick device;
directInput = new DirectInput();
//接続されているキーボードを探す
Guid keyboardGuid = Guid.Empty;
foreach (var g in directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices))
{
if (g.ProductGuid.ToString() == "80481ccf-0000-0000-0000-504944564944")
{
keyboardGuid = g.InstanceGuid;
break;
}
}
//キーボードが見つからない時は終了する
if (keyboardGuid == Guid.Empty)
{
Console.WriteLine("キーボードが見つかりません。終了します。");
System.Threading.Thread.Sleep(5000);
return;
}
//デバイスを初期化
device = new Joystick(directInput, keyboardGuid);
//キー入力の検知開始
device.Acquire();
int x = device.GetCurrentState().X;
ulong counter = 1;
bool isActive = false;
bool isRight = false;
//ここから入力の検知を開始
while (true)
{
//現在の入力状況を取得する
var state = device.GetCurrentState();
int stateX = state.X;
if (x != stateX)
{
//Console.WriteLine(System.DateTime.Now.ToString() + ":「" + state.X + "」");
bool nowRight = false;
if(x < stateX)
{
nowRight = true;
if ((stateX - x) > (65535 - stateX + x))
{
nowRight = false;
}
}
else if(x > stateX)
{
nowRight = false;
if ((x - stateX) > ((stateX + 65535) - x))
{
nowRight = true;
}
}
if(isActive && !(isRight == nowRight))
{
// 方向が切り替わった
if (isRight)
{
keybd_event(LEFT_SHIFT, 0, 2u, (UIntPtr)0uL);
keybd_event(LEFT_CTRL, 0, 0u, (UIntPtr)0uL);
Console.WriteLine("Left start");
}
else
{
keybd_event(LEFT_CTRL, 0, 2u, (UIntPtr)0uL);
keybd_event(LEFT_SHIFT, 0, 0u, (UIntPtr)0uL);
Console.WriteLine("Right start");
}
isRight = nowRight;
Console.WriteLine("Change!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
}
else if (!isActive)
{
// 回しはじめ
if (nowRight)
{
keybd_event(LEFT_SHIFT, 0, 0u, (UIntPtr)0uL);
Console.WriteLine("Right start");
}
else
{
keybd_event(LEFT_CTRL, 0, 0u, (UIntPtr)0uL);
Console.WriteLine("Left start");
}
isActive = true;
isRight = nowRight;
}
counter = 0;
x = stateX;
}
if(counter > 500000 && isActive)
{
if (isRight)
{
keybd_event(LEFT_SHIFT, 0, 2u, (UIntPtr)0uL);
}
else
{
keybd_event(LEFT_CTRL, 0, 2u, (UIntPtr)0uL);
}
isActive = false;
counter = 0;
}
if (counter == ulong.MaxValue)
{
counter = 0;
}
counter++;
}
}
[DllImport("user32.dll")]
public static extern uint keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
private static byte LEFT_SHIFT = 0xA0;
private static byte LEFT_CTRL = 0xA2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment