Which HID key code to use for KEYCODE_BACK button mapping? - python-3.x

Platform:
STB Xiaomi Mi Box S 4
OS Version:
Android Version 9
Issue description:
I want to use the USB keyboard gadget to control the box. I mapped the remote controller buttons (arrow buttons/select/home) into corresponding HID key codes following the page. However, none of the specified/corresponding key codes (0x00f1, 0x009e) for the KEYCODE_BACK button is working as expected.
Question:
Do you maybe know which HID key code should be used for the BACK button?
Appreciated any help!

I found detailed document about HID usages. I hope you can find your answer from the pdf.
PDF: https://usb.org/sites/default/files/hut1_21.pdf

You may use the following code to determine if the Key is Back / Menu on the Android device / key code
if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK || event.getKeyCode() == KeyEvent.KEYCODE_MENU || event.getKeyCode() == KeyEvent.KEYCODE_BUTTON_MODE) && event.getRepeatCount() == 0) {
if( onBackPressed() ) { // function to determine if we should block the action in your code and do something else return true if you want to proc
return true;
} else {
// let the Android system handle the back button
return false;
}
}

Related

After taking Picture, how to select Ok in appium when tick mark cant be located?

After taking Picture, how to select Ok in appium when tick mark cant be located?
only camara layout is Higlighted and i am not able to loacte tick mark or revert.
Please try below code snippet. If you are able to click using android keyboard then below code should work.
((AndroidDriver)driver).pressKey(new KeyEvent(AndroidKey.SEARCH));
I acheived this using TouchAction.
TouchAction touchAction = new TouchAction(driver);
touchAction.tap(new PointOption().withCoordinates(111, 222)).perform()
solution for accepting image using camera app in appium.
keyEvent=>27, is use to capture the image using camera app.
i used below code to accept the captured image. (but its not working)
i used keyEvent=> 66, Enter button to accept the image (but its not working).
i used keyEvent=> 84, Space button to accept the image (but its not working).
below working code.
i used keyEvent=> 22 and then i used keyEvent=> 66, it will accept the image.
$app.press_keycode(27) #click on camera accept button.
$app.press_keycode(22)
$app.press_keycode(66)
sleep 2
$app.press_keycode(27)
$app.press_keycode(22)
$app.press_keycode(66)
Note: repeat the code for 2 twice
i used keyEvent=> 21 and then i used keyEvent=> 66 and i used keyEvent=> 27, it will allow u to retake the image.
i used keyEvent=>4, it will take back u form camera app
The way I handled Camera and Tick button initially was using Android Key Codes:
Thread.sleep(2000);
androidDriver.pressKey(new KeyEvent(AndroidKey.CAMERA));
Thread.sleep(1000);
androidDriver.pressKey(new KeyEvent(AndroidKey.TAB));
Thread.sleep(1000);
androidDriver.pressKey(new KeyEvent(AndroidKey.TAB));
Thread.sleep(1000);
androidDriver.pressKey(new KeyEvent(AndroidKey.ENTER));
In most of the devices which I researched earlier, the above snippet worked like a charm for me. However, when I started testing on Android versions 9, 10 and 11 recently, this wasn't working and the number of tabs increased to 3 or 4. To handle this in most of the devices like Samsung, OnePlus, Google Pixel, I refactored the code as:
Thread.sleep(2000);
// Click Camera shutter button
androidDriver.pressKey(new KeyEvent(AndroidKey.CAMERA));
try {
WebDriverWait wait = new WebDriverWait(androidDriver, 10);
wait.until(ExpectedConditions.elementToBeClickable((MobileBy.AccessibilityId("Done");
// Click Done or Tick after clicking photo (Mostly visible in OnePlus, Nokia, Google Pixel devices)
androidDriver.findElementByAccessibilityId("Done").click();
} catch (NoSuchElementException e) {
flag = true;
System.out.println("NoSuchElement for DONE Button");
}
if (flag) {
try {
// Click OK after clicking photo (Mostly visible in Samsung devices)
// No need to put WebDriverWait here as we already waited for 10 secs after clicking on Camera
androidDriver.findElementByXPath("//android.widget.TextView[#text='OK']").click();
flag = false;
System.out.println("OK Button Clicked");
} catch (NoSuchElementException e) {
flag = true;
System.out.println("NoSuchElement for OK Button");
}
}
if (flag) {
// If both above methods failed, then this may work (Mostly works for Xiaomi, Nokia devices)
androidDriver.pressKey(new KeyEvent(AndroidKey.TAB));
hardStopWait(1000);
androidDriver.pressKey(new KeyEvent(AndroidKey.TAB));
hardStopWait(1000);
androidDriver.pressKey(new KeyEvent(AndroidKey.ENTER));
System.out.println("TABS & ENTER Pressed");
}

Xamarin UITest - determine if device is Phone or Tablet for Android?

Is there a way to determine if the Android device I'm running tests on is Phone or Tablet? Found various answers on this topic from an Android development perspective, but I'm just developing tests and looking for some method from Xamarin.UITest that could tell me this. For the iOS there is a built-in method like app.Device.IsPhone, but I can't seem to find something similar for the Android.
Any ideas?
The way that I do it in my tests is check the size of the device see code below:
public bool DeviceIsTablet()
{
var screen = app.Query(x => x.Id("content"));
var height = screen.FirstOrDefault().Rect.Height;
var width = screen.FirstOrDefault().Rect.Width;
if (width < 1600 || height > 1850)
{
return false;
}
else
{
return true;
}
}

How to know when focus changes in a CDialog derived class

"Common Controls" send out a NM_SETFOCUS notification, but not basic controls like an edit control.
Is there a way in my CDialog-derived class to know when focus changes to ANY control in my dialog? If not within my dialog, then possibly ANY focus change (I can figure it out from the hwnd)?
You should be able to handle CWnd::OnCommand and trap the command ID and notification message. I'd try something like this...
if((notificationCode == EN_KILLFOCUS) ||
(notificationCode == LBN_KILLFOCUS) ||
(notificationCode == CBN_KILLFOCUS) ||
(notificationCode == NM_KILLFOCUS) ||
(notificationCode == WM_KILLFOCUS))
{
// Here do whatever you want.
}
You can expand it by adding the equivalent _SETFOCUS notifications.

get OpenFileDialog button pressed

I'm using an OpenFileDialog to let the user chose a file to open.
it works, but if I chose a file and insted of pressing OK I press CANCEL it still opens the file because I picked up one.
I found this code in the MSDN, but I can't see ::DialogResult::OK in my apllication
if ( openFileDialog1->ShowDialog() == ::DialogResult::OK )
{
if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
{
// Insert code to read the stream here.
myStream->Close();
}
}
my code is:
fileD1->ShowDialog();
while(!fileD->FileName->Lenght!=0)
{
}
and here I open the file
any other idea of how to know which button I pressed?
I'm using c++ and visual studio 2008
save the return of this ->> openFileDialog1->ShowDialog()
into a value and then check the value
DialogResult::OK
or
DialogResult::CANCEL

In Lwuit, How to handle Keypad navigation Events?

In the application I'm building I have Tabs and a list in for each Tab. the behavior I want is when I press the Left/Right Nav Key the selected Tab will change. when I press the Up/Down Nav Key the List will change selection index.
I used LWUIT GUI builder to generate StateMachine/StateMachineBase class.
I've been trying to fix this all day. please help.
Pheromix's answer is correct but he didn't account for the UIBuilder. To override the Form creation in the UIBuilder override the method:
protected Component createComponentInstance(String componentType, Class cls) {
if(cls == com.sun.lwuit.Form.class) {
return new MyFormSubclass();
}
return super.createComponentInstance(componentType, cls);
}
There is an option to override the Tab selection behavior but to get the most accurate behavior this might be the best approach.
Why do you complicate your life ? Just use keyReleased method implementation in your class. Make a test :
if (display.getGameAction(keyCode) == Display.GAME_LEFT || display.getGameAction(keyCode) == Display.GAME_RIGHT)
{
if (tab.getSelectedIndex == 0)
tab.setSelectedIndex(1);
else
tab.setSelectedIndex(0);
}
else if (display.getGameAction(keyCode) == Display.GAME_UP || display.getGameAction(keyCode) == Display.GAME_DOWN)
{
if (list.hasFocus())
super.keyReleased(keyCode);
}

Resources