Skip to content

Commit 9cb1727

Browse files
committed
refactor & feat: Controller.ClickKey() & KeyDown() & KeyUp()
MaaXYZ/MaaFramework#724
1 parent ef4abf6 commit 9cb1727

5 files changed

Lines changed: 94 additions & 16 deletions

File tree

src/MaaFramework.Binding.Native/MaaController.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,23 @@ public MaaJob Swipe(int x1, int y1, int x2, int y2, int duration)
111111
/// <remarks>
112112
/// Wrapper of <see cref="MaaControllerPostPressKey"/>.
113113
/// </remarks>
114+
[Obsolete("Use ClickKey() instead.")]
114115
public MaaJob PressKey(int keyCode)
115116
{
116117
var id = MaaControllerPostPressKey(Handle, keyCode);
117118
return LastJob = new MaaJob(id, this);
118119
}
119120

121+
/// <inheritdoc/>
122+
/// <remarks>
123+
/// Wrapper of <see cref="MaaControllerPostClickKey"/>.
124+
/// </remarks>
125+
public MaaJob ClickKey(int keyCode)
126+
{
127+
var id = MaaControllerPostClickKey(Handle, keyCode);
128+
return LastJob = new MaaJob(id, this);
129+
}
130+
120131
/// <inheritdoc/>
121132
/// <remarks>
122133
/// Wrapper of <see cref="MaaControllerPostInputText"/>.
@@ -177,6 +188,26 @@ public MaaJob TouchUp(int contact)
177188
return LastJob = new MaaJob(id, this);
178189
}
179190

191+
/// <inheritdoc/>
192+
/// <remarks>
193+
/// Wrapper of <see cref="MaaControllerPostKeyDown"/>.
194+
/// </remarks>
195+
public MaaJob KeyDown(int keyCode)
196+
{
197+
var id = MaaControllerPostKeyDown(Handle, keyCode);
198+
return LastJob = new MaaJob(id, this);
199+
}
200+
201+
/// <inheritdoc/>
202+
/// <remarks>
203+
/// Wrapper of <see cref="MaaControllerPostKeyUp"/>.
204+
/// </remarks>
205+
public MaaJob KeyUp(int keyCode)
206+
{
207+
var id = MaaControllerPostKeyUp(Handle, keyCode);
208+
return LastJob = new MaaJob(id, this);
209+
}
210+
180211
/// <inheritdoc/>
181212
/// <remarks>
182213
/// Wrapper of <see cref="MaaControllerPostScreencap"/>.

src/MaaFramework.Binding.UnitTests/Test_Custom.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ public bool Connect()
187187
public bool InputText(string text)
188188
=> c.InputText(text).Wait() == MaaJobStatus.Succeeded;
189189

190-
public bool PressKey(int keycode)
191-
=> c.PressKey(keycode).Wait() == MaaJobStatus.Succeeded;
190+
public bool ClickKey(int keycode)
191+
=> c.ClickKey(keycode).Wait() == MaaJobStatus.Succeeded;
192192

193193
public bool RequestResolution(out int width, out int height)
194194
{
@@ -213,25 +213,30 @@ public bool RequestUuid(in IMaaStringBuffer buffer)
213213
}
214214

215215
public bool Screencap(in IMaaImageBuffer buffer)
216-
=> c.Screencap().Wait() == MaaJobStatus.Succeeded && c.GetCachedImage(buffer);
216+
=> c.Screencap().Wait().IsSucceeded() && c.GetCachedImage(buffer);
217217

218218
public bool StartApp(string intent)
219-
=> c.StartApp(intent).Wait() == MaaJobStatus.Succeeded;
219+
=> c.StartApp(intent).Wait().IsSucceeded();
220220

221221
public bool StopApp(string intent)
222-
=> c.StopApp(intent).Wait() == MaaJobStatus.Succeeded;
222+
=> c.StopApp(intent).Wait().IsSucceeded();
223223

224224
public bool Swipe(int x1, int y1, int x2, int y2, int duration)
225-
=> c.Swipe(x1, y1, x2, y2, duration).Wait() == MaaJobStatus.Succeeded;
225+
=> c.Swipe(x1, y1, x2, y2, duration).Wait().IsSucceeded();
226226

227227
public bool TouchDown(int contact, int x, int y, int pressure)
228-
=> c.TouchDown(contact, x, y, pressure).Wait() == MaaJobStatus.Succeeded;
228+
=> c.TouchDown(contact, x, y, pressure).Wait().IsSucceeded();
229229

230230
public bool TouchMove(int contact, int x, int y, int pressure)
231-
=> c.TouchMove(contact, x, y, pressure).Wait() == MaaJobStatus.Succeeded;
231+
=> c.TouchMove(contact, x, y, pressure).Wait().IsSucceeded();
232232

233233
public bool TouchUp(int contact)
234-
=> c.TouchUp(contact).Wait() == MaaJobStatus.Succeeded;
234+
=> c.TouchUp(contact).Wait().IsSucceeded();
235+
236+
public bool KeyDown(int keycode)
237+
=> c.KeyDown(keycode).Wait().IsSucceeded();
238+
public bool KeyUp(int keycode)
239+
=> c.KeyUp(keycode).Wait().IsSucceeded();
235240
}
236241

237242
internal sealed class TestInvalidResource : IMaaCustomResource

src/MaaFramework.Binding.UnitTests/Test_IMaaController.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ public void Interface_Swipe(MaaTypes type, IMaaController maaController, bool as
208208

209209
[TestMethod]
210210
[MaaData(MaaTypes.All, nameof(Data), true, 4)] // KEYCODE_BACK
211-
public void Interface_PressKey(MaaTypes type, IMaaController maaController, bool assertSuccess, int keyCode)
211+
public void Interface_ClickKey(MaaTypes type, IMaaController maaController, bool assertSuccess, int keyCode)
212212
{
213213
Assert.IsNotNull(maaController);
214214

215-
var job = maaController.PressKey(keyCode);
215+
var job = maaController.ClickKey(keyCode);
216216
Interface_IMaaPost(assertSuccess, job);
217217
}
218218

@@ -266,6 +266,23 @@ public void Interface_TouchDown_TouchMove_TouchUp(MaaTypes type, IMaaController
266266
Interface_IMaaPost(assertSuccess, job);
267267
}
268268

269+
[TestMethod]
270+
[MaaData(MaaTypes.All, nameof(Data), false, 4, "Adb")]
271+
[MaaData(MaaTypes.All, nameof(MiniTouchData), false, 4, "MiniTouch")]
272+
[MaaData(MaaTypes.All, nameof(MaaTouchData), true, 4, "MaaTouch")]
273+
public void Interface_KeyDown_KeyUp(MaaTypes type, IMaaController maaController, bool assertSuccess, int keyCode, string adbControllerTypes)
274+
{
275+
Assert.IsNotNull(maaController);
276+
277+
var job = maaController.KeyDown(keyCode);
278+
Interface_IMaaPost(assertSuccess, job);
279+
Task.Delay(100).Wait();
280+
281+
job = maaController.KeyUp(keyCode);
282+
Interface_IMaaPost(assertSuccess, job);
283+
Task.Delay(100).Wait();
284+
}
285+
269286
private static MaaImageBuffer GetImage(MaaTypes type, IMaaController maaController)
270287
{
271288
var job = maaController.Screencap();

src/MaaFramework.Binding/Custom/IMaaCustomController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public interface IMaaCustomController : IMaaCustomResource, IDisposable
3232
bool TouchDown(int contact, int x, int y, int pressure);
3333
bool TouchMove(int contact, int x, int y, int pressure);
3434
bool TouchUp(int contact);
35-
bool PressKey(int keycode);
35+
bool ClickKey(int keycode);
3636
bool InputText(string text);
37+
bool KeyDown(int keycode);
38+
bool KeyUp(int keycode);
3739
}

src/MaaFramework.Binding/IMaaController.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,23 @@ public interface IMaaController : IMaaCommon, IMaaOption<ControllerOption>, IMaa
4141

4242
/// <summary>
4343
/// Presses a key.
44+
/// <para>For adb controller, <paramref name="keyCode"/> is from <a href="https://developer.android.com/reference/android/view/KeyEvent">android key event</a>.</para>
45+
/// <para>For win32 controller, <paramref name="keyCode"/> is from <a href="https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes">windows virtual key</a>.</para>
4446
/// </summary>
4547
/// <param name="keyCode">The code of the key.</param>
4648
/// <returns>A press key <see cref="MaaJob"/>.</returns>
49+
[Obsolete("Use ClickKey() instead.")]
4750
MaaJob PressKey(int keyCode);
4851

52+
/// <summary>
53+
/// Clicks a key.
54+
/// <para>For adb controller, <paramref name="keyCode"/> is from <a href="https://developer.android.com/reference/android/view/KeyEvent">android key event</a>.</para>
55+
/// <para>For win32 controller, <paramref name="keyCode"/> is from <a href="https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes">windows virtual key</a>.</para>
56+
/// </summary>
57+
/// <param name="keyCode">The code of the key.</param>
58+
/// <returns>A click key <see cref="MaaJob"/>.</returns>
59+
MaaJob ClickKey(int keyCode);
60+
4961
/// <summary>
5062
/// Inputs a text.
5163
/// </summary>
@@ -73,11 +85,9 @@ public interface IMaaController : IMaaCommon, IMaaOption<ControllerOption>, IMaa
7385

7486
/// <summary>
7587
/// Usage: TouchDown -> TouchMove -> TouchUp.
88+
/// <para>For adb controller, <paramref name="contact"/> means finger id (0 for first finger, 1 for second finger, etc.).</para>
89+
/// <para>For win32 controller, <paramref name="contact"/> means mouse button id (0 for left, 1 for right, 2 for middle).</para>
7690
/// </summary>
77-
/// <remarks>
78-
/// <para>For adb controller, contact means finger id (0 for first finger, 1 for second finger, etc.).</para>
79-
/// <para>For win32 controller, contact means mouse button id (0 for left, 1 for right, 2 for middle).</para>
80-
/// </remarks>
8191
/// <param name="contact">The contact id.</param>
8292
/// <param name="x">The horizontal coordinate of the point.</param>
8393
/// <param name="y">The vertical coordinate of the point.</param>
@@ -93,6 +103,19 @@ public interface IMaaController : IMaaCommon, IMaaOption<ControllerOption>, IMaa
93103
/// <inheritdoc cref="TouchDown"/>
94104
MaaJob TouchUp(int contact);
95105

106+
/// <summary>
107+
/// Usage: KeyDown -> KeyUp.
108+
/// <para>For adb controller, <paramref name="keyCode"/> is from <a href="https://developer.android.com/reference/android/view/KeyEvent">android key event</a>.</para>
109+
/// <para>For win32 controller, <paramref name="keyCode"/> is from <a href="https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes">windows virtual key</a>.</para>
110+
/// </summary>
111+
/// <param name="keyCode">The code of the key.</param>
112+
/// <returns>A key down <see cref="MaaJob"/>.</returns>
113+
MaaJob KeyDown(int keyCode);
114+
115+
/// <returns>A key up <see cref="MaaJob"/>.</returns>
116+
/// <inheritdoc cref="KeyDown"/>
117+
MaaJob KeyUp(int keyCode);
118+
96119
/// <summary>
97120
/// Takes a screenshot.
98121
/// </summary>

0 commit comments

Comments
 (0)