This is the method I have created, which uses key codes and key names. I wrote this code about 10 years ago and back then it supported most devices. (One exception I found, however, was some Sagem models which have the -6
and -7
key codes the other way round! But you could probably work around that using the key names again - but you may need to obtain the user agent too.)
private static final int SOFT_BUTTON_KEY_CODE_UNDEFINED = -999;
private static int LEFT_SOFT_BUTTON_KEY_CODE = SOFT_BUTTON_KEY_CODE_UNDEFINED;
private static int RIGHT_SOFT_BUTTON_KEY_CODE = SOFT_BUTTON_KEY_CODE_UNDEFINED;
private boolean isLeftSoftButton(int keyCode) {
// Try the standard code
if (keyCode == -6) {
return true;
}
// Try the code we have already detected
else if (keyCode == LEFT_SOFT_BUTTON_KEY_CODE && LEFT_SOFT_BUTTON_KEY_CODE != SOFT_BUTTON_KEY_CODE_UNDEFINED) {
return true;
}
// If we haven't yet detected the code...
else if (LEFT_SOFT_BUTTON_KEY_CODE == SOFT_BUTTON_KEY_CODE_UNDEFINED) {
// try to detect it
String keyName = getKeyName(keyCode).toUpperCase();
if (keyName.equals("SOFT1") || keyName.equals("LEFT SELECTION KEY") || keyName.equals("LEFT SOFTKEY") || keyName.equals("LEFT SOFT KEY") || keyName.equals("SOFTKEY 1") || keyName.equals("-6")) {
// It's the left soft button! So remember the code for next time...
LEFT_SOFT_BUTTON_KEY_CODE = keyCode;
// Return true
return true;
}
else {
// keyName didn't match, so return false
return false;
}
}
else {
// keyCode didn't match
return false;
}
}
private boolean isRightSoftButton(int keyCode) {
// Try the standard code
if (keyCode == -7) {
return true;
}
// Try the code we have already detected
else if (keyCode == RIGHT_SOFT_BUTTON_KEY_CODE && RIGHT_SOFT_BUTTON_KEY_CODE != SOFT_BUTTON_KEY_CODE_UNDEFINED) {
return true;
}
// If we haven't yet detected the code...
else if (RIGHT_SOFT_BUTTON_KEY_CODE == SOFT_BUTTON_KEY_CODE_UNDEFINED) {
// try to detect it
String keyName = getKeyName(keyCode).toUpperCase();
if (keyName.equals("SOFT2") || keyName.equals("RIGHT SELECTION KEY") || keyName.equals("RIGHT SOFTKEY") || keyName.equals("RIGHT SOFT KEY") || keyName.equals("SOFTKEY 4") || keyName.equals("SOFTKEY 2") || keyName.equals("-7")) {
// It's the right soft button! So remember the code for next time...
RIGHT_SOFT_BUTTON_KEY_CODE = keyCode;
// Return true
return true;
}
else {
// keyName didn't match, so return false
return false;
}
}
else {
// keyCode didn't match
return false;
}
}
Updated code, based on http://www.iteye.com/topic/179073 ...
private static final int SOFT_BUTTON_KEY_CODE_UNDEFINED = -999;
private static int LEFT_SOFT_BUTTON_KEY_CODE = SOFT_BUTTON_KEY_CODE_UNDEFINED;
private static int RIGHT_SOFT_BUTTON_KEY_CODE = SOFT_BUTTON_KEY_CODE_UNDEFINED;
private boolean isLeftSoftButton(int keyCode) {
// Try the standard codes
// standard || Motorola || Siemens || Motorola 2 || Motorola 1
if (keyCode == -6 || keyCode == -21 || keyCode == -1 || keyCode == -20 || keyCode == 21) {
return true;
}
// Try the code we have already detected
else if (keyCode == LEFT_SOFT_BUTTON_KEY_CODE && LEFT_SOFT_BUTTON_KEY_CODE != SOFT_BUTTON_KEY_CODE_UNDEFINED) {
return true;
}
// If we haven't yet detected the code...
else if (LEFT_SOFT_BUTTON_KEY_CODE == SOFT_BUTTON_KEY_CODE_UNDEFINED) {
// try to detect it
String keyName = getKeyName(keyCode).toUpperCase();
if (keyName.equals("SOFT1") || keyName.equals("LEFT SELECTION KEY") || keyName.equals("LEFT SOFTKEY") || keyName.equals("LEFT SOFT KEY") || keyName.equals("SOFTKEY 1") || keyName.equals("-6")) {
// It's the left soft button! So remember the code for next time...
LEFT_SOFT_BUTTON_KEY_CODE = keyCode;
// Return true
return true;
}
else {
// keyName didn't match, so return false
return false;
}
}
else {
// keyCode didn't match
return false;
}
}
private boolean isRightSoftButton(int keyCode) {
// Try the standard codes
// standard || Motorola || Siemens || Motorola 1
if (keyCode == -7 || keyCode == -22 || keyCode == -4 || keyCode == 22) {
return true;
}
// Try the code we have already detected
else if (keyCode == RIGHT_SOFT_BUTTON_KEY_CODE && RIGHT_SOFT_BUTTON_KEY_CODE != SOFT_BUTTON_KEY_CODE_UNDEFINED) {
return true;
}
// If we haven't yet detected the code...
else if (RIGHT_SOFT_BUTTON_KEY_CODE == SOFT_BUTTON_KEY_CODE_UNDEFINED) {
// try to detect it
String keyName = getKeyName(keyCode).toUpperCase();
if (keyName.equals("SOFT2") || keyName.equals("RIGHT SELECTION KEY") || keyName.equals("RIGHT SOFTKEY") || keyName.equals("RIGHT SOFT KEY") || keyName.equals("SOFTKEY 4") || keyName.equals("SOFTKEY 2") || keyName.equals("-7")) {
// It's the right soft button! So remember the code for next time...
RIGHT_SOFT_BUTTON_KEY_CODE = keyCode;
// Return true
return true;
}
else {
// keyName didn't match, so return false
return false;
}
}
else {
// keyCode didn't match
return false;
}
}`