Skip to content
This repository has been archived by the owner on Dec 21, 2022. It is now read-only.

Commit

Permalink
模拟按键需要管理员权限问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex1911-Jiang committed Mar 21, 2021
1 parent a9e7eee commit d3340ca
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 230 deletions.
12 changes: 11 additions & 1 deletion GenshinBalladsOfBreezeAider/GenshinBalladsOfBreezeAider.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<TargetZone>
</TargetZone>
</PropertyGroup>
<PropertyGroup>
<GenerateManifests>false</GenerateManifests>
</PropertyGroup>
<PropertyGroup>
<ApplicationManifest>Properties\app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -55,7 +65,6 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WinIO32.cs" />
<EmbeddedResource Include="frmMain.resx">
<DependentUpon>frmMain.cs</DependentUpon>
</EmbeddedResource>
Expand All @@ -68,6 +77,7 @@
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<None Include="Properties\app.manifest" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
18 changes: 17 additions & 1 deletion GenshinBalladsOfBreezeAider/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Principal;
using System.Threading.Tasks;
using System.Windows.Forms;

Expand All @@ -16,7 +18,21 @@ static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
Application.Run(new frmMain());
}
else
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = Application.ExecutablePath;
startInfo.Arguments = string.Join(" ", "");
startInfo.Verb = "runas";
Process.Start(startInfo);
Application.Exit();
}
}
}
}
215 changes: 0 additions & 215 deletions GenshinBalladsOfBreezeAider/WinIO32.cs

This file was deleted.

46 changes: 33 additions & 13 deletions GenshinBalladsOfBreezeAider/frmMain.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Management;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -169,6 +170,8 @@ private void btnStart_Click(object sender, EventArgs e)
float dpiX = DpiScaleX;
float dpiY = DpiScaleY;

Dictionary<Keys, DateTime> dicKeysNextPressTime = new Dictionary<Keys, DateTime>();

while (working)
{
int width = (int)Math.Round(Screen.PrimaryScreen.Bounds.Width * dpiX);
Expand All @@ -178,14 +181,14 @@ private void btnStart_Click(object sender, EventArgs e)
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);

StartAutoPressKey(memoryImage, 0.253125, 0.37685, Keys.W);
StartAutoPressKey(memoryImage, 0.253125, 0.823148, Keys.S);
StartAutoPressKey(memoryImage, 0.127083, 0.6, Keys.A);
StartAutoPressKey(memoryImage, 0.378125, 0.6, Keys.D);
StartAutoPressKey(memoryImage, 0.74739583, 0.37685, Keys.I);
StartAutoPressKey(memoryImage, 0.74739583, 0.823148, Keys.K);
StartAutoPressKey(memoryImage, 0.62135416, 0.6, Keys.J);
StartAutoPressKey(memoryImage, 0.872917, 0.6, Keys.L);
StartAutoPressKeyAndRecordCD(dicKeysNextPressTime, memoryImage, 0.253125, 0.37685, Keys.W);
StartAutoPressKeyAndRecordCD(dicKeysNextPressTime, memoryImage, 0.253125, 0.823148, Keys.S);
StartAutoPressKeyAndRecordCD(dicKeysNextPressTime, memoryImage, 0.127083, 0.6, Keys.A);
StartAutoPressKeyAndRecordCD(dicKeysNextPressTime, memoryImage, 0.378125, 0.6, Keys.D);
StartAutoPressKeyAndRecordCD(dicKeysNextPressTime, memoryImage, 0.74739583, 0.37685, Keys.I);
StartAutoPressKeyAndRecordCD(dicKeysNextPressTime, memoryImage, 0.74739583, 0.823148, Keys.K);
StartAutoPressKeyAndRecordCD(dicKeysNextPressTime, memoryImage, 0.62135416, 0.6, Keys.J);
StartAutoPressKeyAndRecordCD(dicKeysNextPressTime, memoryImage, 0.872917, 0.6, Keys.L);

memoryGraphics.Dispose();
memoryImage.Dispose();
Expand All @@ -195,6 +198,21 @@ private void btnStart_Click(object sender, EventArgs e)

bool wReady, sReady, aReady, dReady, iReady, kReady, jReady, lReady;

private void StartAutoPressKeyAndRecordCD(Dictionary<Keys, DateTime> dicKeysNextPressTime, Bitmap bmp, double scaleX, double scaleY, Keys key)
{
if (dicKeysNextPressTime.ContainsKey(key))
{
if (dicKeysNextPressTime[key] < DateTime.Now)
{
dicKeysNextPressTime[key] = StartAutoPressKey(bmp, scaleX, scaleY, key);
}
}
else
{
dicKeysNextPressTime.Add(key, StartAutoPressKey(bmp, scaleX, scaleY, key));
}
}

private void SaveDebugImage()
{
float dpiX = DpiScaleX;
Expand Down Expand Up @@ -233,8 +251,9 @@ private void DrawDebugLine(double scaleX, double scaleY, Bitmap highDpiScreensho
}
}

private void StartAutoPressKey(Bitmap bmp, double scaleX, double scaleY, Keys key)
private DateTime StartAutoPressKey(Bitmap bmp, double scaleX, double scaleY, Keys key)
{
DateTime nextEnablePressTime;
bool getReady = key switch
{
Keys.W => wReady,
Expand All @@ -250,7 +269,7 @@ private void StartAutoPressKey(Bitmap bmp, double scaleX, double scaleY, Keys ke

int x = (int)Math.Round(genshinWindowWdith * scaleX) + genshinWindowX;
int y = (int)Math.Round(genshinWindowHeight * scaleY) + genshinWindowY;
PressKey(ref getReady, bmp, x, y, key);
nextEnablePressTime = PressKey(ref getReady, bmp, x, y, key);

switch (key)
{
Expand Down Expand Up @@ -279,6 +298,7 @@ private void StartAutoPressKey(Bitmap bmp, double scaleX, double scaleY, Keys ke
lReady = getReady;
break;
}
return nextEnablePressTime;
}

private byte GetScancode(Keys key) => key switch
Expand All @@ -294,7 +314,7 @@ private void StartAutoPressKey(Bitmap bmp, double scaleX, double scaleY, Keys ke
_ => throw new Exception()
};

private void PressKey(ref bool getReady, Bitmap bmp, int x, int y, Keys key)
private DateTime PressKey(ref bool getReady, Bitmap bmp, int x, int y, Keys key)
{
Color color = bmp.GetPixel(x, y);
if (!getReady)
Expand All @@ -312,15 +332,15 @@ private void PressKey(ref bool getReady, Bitmap bmp, int x, int y, Keys key)
//getReady = false; //连击不会显示黑色圈导致判定有问题, 现只用作判断是否开始音游
//byte code = GetScancode(key);
keybd_event(byteKey, 0, 0, 0);
//WinIo.MykeyDown(byteKey);
//Invoke(new Action(() => debugTextBox.AppendText($"按下按键{((Keys)key).ToString()} ----{DateTime.Now}\r\n")));
Task.Delay(50).ContinueWith(_ =>
{
keybd_event(byteKey, 0, 2, 0);
//WinIo.MykeyUp(byteKey);
});
return DateTime.Now.AddMilliseconds(50);
}
}
return DateTime.Now;
}
}
}

0 comments on commit d3340ca

Please sign in to comment.