Skip to content

Commit

Permalink
Utilisation de WindowsInput
Browse files Browse the repository at this point in the history
  • Loading branch information
FLOBOY-France committed Jun 9, 2021
1 parent b842823 commit 1bddbba
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 33 deletions.
20 changes: 13 additions & 7 deletions GamePadToKeyboard/Classes/KeyboardSender.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using WindowsInput;

namespace GamePadToKeyboard
{
Expand All @@ -8,31 +9,36 @@ public class KeyboardSender
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void keybd_event(uint bVk, uint bScan, uint dwFlags, uint dwExtraInfo);

private const int VK_Return = 0x0D; //Constante pour la touche Return
private const int VK_Return = 0x39; //Constante pour la touche Return

public const ushort WM_KEYDOWN = 0x0100;

public const ushort WM_KEYUP = 0x0101;

[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
static extern IntPtr GetForegroundWindow();


[DllImport("user32.dll")] //sends a windows message to the specified window
public static extern int SendMessage(IntPtr hWnd, int Msg, uint wParam, int lParam);


public static void SendReturn()
{
keybd_event(VK_Return, 0, 0, 0);
//keybd_event(VK_Return, 0, 0, 0);
InputSimulator sim = new InputSimulator();
sim.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.RETURN);

}

public static void SendKey(uint keyCode)
{
//keybd_event(keyCode, 0, 0, 0); //Simuler autre touche, liste disponible ici : http://www.kbdedit.com/manual/low_level_vk_list.html
SendMessage(GetActiveWindow(), WM_KEYUP, VK_Return, 0);
SendMessage(GetActiveWindow(), WM_KEYDOWN, VK_Return, 0);
SendMessage(GetActiveWindow(), WM_KEYUP, VK_Return, 0);

//SendMessage(GetForegroundWindow(), WM_KEYUP, keyCode, 0);
//SendMessage(GetForegroundWindow(), WM_KEYDOWN, keyCode, 0);
//SendMessage(GetForegroundWindow(), WM_KEYUP, keyCode, 0);


}

}
Expand Down
3 changes: 3 additions & 0 deletions GamePadToKeyboard/GamePadToKeyboard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="WindowsInput">
<HintPath>..\..\inputsimulator\WindowsInput\bin\Debug\WindowsInput.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Classes\KeyboardSender.cs" />
Expand Down
62 changes: 36 additions & 26 deletions GamePadToKeyboard/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,54 @@ static void Main(string[] args)
Console.WriteLine("A : Activation, B : Clear, Back : Sortie de l'application");
Console.WriteLine("Intervalle fixé à " + intervalleMS);

while (true)
int gamePadNumber = 0;

for(int i=0; i<4; i++)
{
GamePadState state = GamePad.GetState(0);
if (GamePad.GetState(i).IsConnected == true)
{
gamePadNumber = i;
Console.WriteLine("Numero de pad : " + i);
break;
}
}

while (true)
{

if (state.Buttons.A.Equals(ButtonState.Released) && previousState.Equals(ButtonState.Pressed))
{

TimeSpan timeSpan = DateTime.Now - dtLastPressed;
if (timeSpan.TotalMilliseconds > intervalleMS)
GamePadState state = GamePad.GetState(gamePadNumber);
if (state.Buttons.A.Equals(ButtonState.Released) && previousState.Equals(ButtonState.Pressed))
{
dtLastPressed = DateTime.Now;
KeyboardSender.SendReturn();
Console.WriteLine("On a relâché A, écart : " + timeSpan.TotalMilliseconds);

TimeSpan timeSpan = DateTime.Now - dtLastPressed;
if (timeSpan.TotalMilliseconds > intervalleMS)
{
dtLastPressed = DateTime.Now;
KeyboardSender.SendReturn();
//KeyboardSender.SendKey(0x0D);
Console.WriteLine("On a relâché A, écart : " + timeSpan.TotalMilliseconds);

}
else
{
Console.WriteLine("Non accepté : " + timeSpan.TotalMilliseconds + " < " + intervalleMS);
}
}
else

else if (state.Buttons.B.Equals(ButtonState.Pressed))
Console.Clear();
else if (state.Buttons.Back.Equals(ButtonState.Pressed))
{
Console.WriteLine("Non accepté : " + timeSpan.TotalMilliseconds + " < " + intervalleMS);
Console.WriteLine("Êtes-vous sûr de vouloir sortir ? o/N");
string rep = Console.ReadLine();
if (rep.ToUpper().Equals("O"))
break;
}
}

else if (state.Buttons.B.Equals(ButtonState.Pressed))
Console.Clear();
else if (state.Buttons.Back.Equals(ButtonState.Pressed))
{
Console.WriteLine("Êtes-vous sûr de vouloir sortir ? o/N");
string rep = Console.ReadLine();
if (rep.ToUpper().Equals("O"))
break;
previousState = state.Buttons.A;
}

previousState = state.Buttons.A;


}
}

}
}

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ C# avec le framework .Net 4.7
## Dépendances
Utilise OpenTk pour la gestion de la manette https://github.com/opentk et particulièrement GLControl.
Il semble y'avoir un problème de dépendances avec les paquets nuggets, Pour installer la bonne version, lancer la commande Install-Package OpenTK.GLControl
Utilise également le projet https://github.com/michaelnoonan/inputsimulator à partir de la 0.03

Liste des touches : http://www.kbdedit.com/manual/low_level_vk_list.html

0 comments on commit 1bddbba

Please sign in to comment.