fix: unload android device screen

This commit is contained in:
lilong.129
2024-12-19 12:12:22 +08:00
parent b1eda1abc1
commit 75f3853b79
3 changed files with 53 additions and 33 deletions

View File

@@ -1 +1 @@
v5.0.0+2412182148
v5.0.0+2412191212

View File

@@ -279,7 +279,18 @@ func (ad *adbDriver) Homescreen() (err error) {
}
func (ad *adbDriver) Unlock() (err error) {
return ad.PressKeyCodes(KCMenu, KMEmpty)
// Notice: brighten should be executed before unlock
// brighten android device screen
if err := ad.PressKeyCodes(KCWakeup, KMEmpty); err != nil {
log.Error().Err(err).Msg("brighten android device screen failed")
}
// unlock android device screen
if err := ad.PressKeyCodes(KCMenu, KMEmpty); err != nil {
log.Error().Err(err).Msg("press menu key to unlock screen failed")
}
// swipe up to unlock
return ad.Swipe(500, 1500, 500, 500)
}
func (ad *adbDriver) Backspace(count int, options ...ActionOption) (err error) {
@@ -287,12 +298,12 @@ func (ad *adbDriver) Backspace(count int, options ...ActionOption) (err error) {
return nil
}
if count == 1 {
return ad.PressKeyCode(67)
return ad.PressKeyCode(KCDel)
}
keyArray := make([]KeyCode, count)
for i := range keyArray {
keyArray[i] = KeyCode(67)
keyArray[i] = KCDel
}
return ad.combinationKey(keyArray)
}
@@ -315,9 +326,18 @@ func (ad *adbDriver) PressKeyCode(keyCode KeyCode) (err error) {
}
func (ad *adbDriver) PressKeyCodes(keyCode KeyCode, metaState KeyMeta) (err error) {
// adb shell input keyevent <keyCode>
_, err = ad.runShellCommand(
"input", "keyevent", fmt.Sprintf("%d", keyCode))
// adb shell input keyevent [--longpress] KEYCODE [METASTATE]
if metaState != KMEmpty {
// press key with metastate, e.g. KMShiftOn/KMCtrlOn
_, err = ad.runShellCommand(
"input", "keyevent", "--longpress",
fmt.Sprintf("%d", keyCode),
fmt.Sprintf("%d", metaState))
} else {
_, err = ad.runShellCommand(
"input", "keyevent",
fmt.Sprintf("%d", keyCode))
}
return
}

View File

@@ -5,32 +5,32 @@ package uixt
type KeyMeta int
const (
KMEmpty KeyMeta = 0 // As a `null`
KMCapLocked KeyMeta = 0x100 // SHIFT key locked in CAPS mode.
KMAltLocked KeyMeta = 0x200 // ALT key locked.
KMSymLocked KeyMeta = 0x400 // SYM key locked.
KMSelecting KeyMeta = 0x800 // Text is in selection mode.
// KMAltOn KeyMeta = 0x02 // This mask is used to check whether one of the ALT meta keys is pressed.
// KMAltLeftOn KeyMeta = 0x10 // This mask is used to check whether the left ALT meta key is pressed.
// KMAltRightOn KeyMeta = 0x20 // This mask is used to check whether the right the ALT meta key is pressed.
// KMShiftOn KeyMeta = 0x1 // This mask is used to check whether one of the SHIFT meta keys is pressed.
// KMShiftLeftOn KeyMeta = 0x40 // This mask is used to check whether the left SHIFT meta key is pressed.
// KMShiftRightOn KeyMeta = 0x80 // This mask is used to check whether the right SHIFT meta key is pressed.
// KMSymOn KeyMeta = 0x4 // This mask is used to check whether the SYM meta key is pressed.
// KMFunctionOn KeyMeta = 0x8 // This mask is used to check whether the FUNCTION meta key is pressed.
// KMCtrlOn KeyMeta = 0x1000 // This mask is used to check whether one of the CTRL meta keys is pressed.
// KMCtrlLeftOn KeyMeta = 0x2000 // This mask is used to check whether the left CTRL meta key is pressed.
// KMCtrlRightOn KeyMeta = 0x4000 // This mask is used to check whether the right CTRL meta key is pressed.
// KMMetaOn KeyMeta = 0x10000 // This mask is used to check whether one of the META meta keys is pressed.
// KMMetaLeftOn KeyMeta = 0x20000 // This mask is used to check whether the left META meta key is pressed.
// KMMetaRightOn KeyMeta = 0x40000 // This mask is used to check whether the right META meta key is pressed.
// KMCapsLockOn KeyMeta = 0x100000 // This mask is used to check whether the CAPS LOCK meta key is on.
// KMNumLockOn KeyMeta = 0x200000 // This mask is used to check whether the NUM LOCK meta key is on.
// KMScrollLockOn KeyMeta = 0x400000 // This mask is used to check whether the SCROLL LOCK meta key is on.
// KMShiftMask = KMShiftOn | KMShiftLeftOn | KMShiftRightOn
// KMAltMask = KMAltOn | KMAltLeftOn | KMAltRightOn
// KMCtrlMask = KMCtrlOn | KMCtrlLeftOn | KMCtrlRightOn
// KMMetaMask = KMMetaOn | KMMetaLeftOn | KMMetaRightOn
KMEmpty KeyMeta = 0 // As a `null`
KMCapLocked KeyMeta = 0x100 // SHIFT key locked in CAPS mode.
KMAltLocked KeyMeta = 0x200 // ALT key locked.
KMSymLocked KeyMeta = 0x400 // SYM key locked.
KMSelecting KeyMeta = 0x800 // Text is in selection mode.
KMAltOn KeyMeta = 0x02 // This mask is used to check whether one of the ALT meta keys is pressed.
KMAltLeftOn KeyMeta = 0x10 // This mask is used to check whether the left ALT meta key is pressed.
KMAltRightOn KeyMeta = 0x20 // This mask is used to check whether the right the ALT meta key is pressed.
KMShiftOn KeyMeta = 0x1 // This mask is used to check whether one of the SHIFT meta keys is pressed.
KMShiftLeftOn KeyMeta = 0x40 // This mask is used to check whether the left SHIFT meta key is pressed.
KMShiftRightOn KeyMeta = 0x80 // This mask is used to check whether the right SHIFT meta key is pressed.
KMSymOn KeyMeta = 0x4 // This mask is used to check whether the SYM meta key is pressed.
KMFunctionOn KeyMeta = 0x8 // This mask is used to check whether the FUNCTION meta key is pressed.
KMCtrlOn KeyMeta = 0x1000 // This mask is used to check whether one of the CTRL meta keys is pressed.
KMCtrlLeftOn KeyMeta = 0x2000 // This mask is used to check whether the left CTRL meta key is pressed.
KMCtrlRightOn KeyMeta = 0x4000 // This mask is used to check whether the right CTRL meta key is pressed.
KMMetaOn KeyMeta = 0x10000 // This mask is used to check whether one of the META meta keys is pressed.
KMMetaLeftOn KeyMeta = 0x20000 // This mask is used to check whether the left META meta key is pressed.
KMMetaRightOn KeyMeta = 0x40000 // This mask is used to check whether the right META meta key is pressed.
KMCapsLockOn KeyMeta = 0x100000 // This mask is used to check whether the CAPS LOCK meta key is on.
KMNumLockOn KeyMeta = 0x200000 // This mask is used to check whether the NUM LOCK meta key is on.
KMScrollLockOn KeyMeta = 0x400000 // This mask is used to check whether the SCROLL LOCK meta key is on.
KMShiftMask = KMShiftOn | KMShiftLeftOn | KMShiftRightOn
KMAltMask = KMAltOn | KMAltLeftOn | KMAltRightOn
KMCtrlMask = KMCtrlOn | KMCtrlLeftOn | KMCtrlRightOn
KMMetaMask = KMMetaOn | KMMetaLeftOn | KMMetaRightOn
)
type KeyFlag int