Reading some articles, told me that soft keys varies between devices.
Some says -6 or -21 for left soft key and -7 or -22 for right soft key.
Given this situation, is there any good wrapper or function or best practice to handle it properly?
If not possible for ALL devices, what is the best way to support most devices? with minor or no hack at all?
To give you a feel for the scope of the problem have a look at this table of keycodes.
Omermuhammed's approach is a good one if you are able to vary the JAD content depending on the target handset, for example by looking at the user-agent header field in a download request from an on-handset web browser.
If you cannot identify the handset until the app has been delivered, you could look at something like this that basically determines the host handset at run time and sets the keycode mappings appropriately.
Looks cumbersome to me though.
Lastly, if your application uses a subset of codes you may be able to get away with hard-coded lists - for some codes there are no or few collisions (LEFT is usually either -3 or -61, and those codes usually don't mean something else). Again, not an ideal approach.
Final suggested resources for you: wurfl or user agent strings, and the J2MEPolish devices database for device keycodes.
The easiest way I found was to set it up in code with the recommended values based on ITU-T standard and override it with a jad parameter. So, for any given app, it will look for existance of the jad parameter at app startup time and set it, otherwise it will use the default values.
I have used these and similar techniques to write apps that can be rapidly ported, and this process is generally well known.
Have to disagree completely with Martin Clayton above, something similar to this method of identifying host handsets at runtime is absolutely the right way to deal with this problem. And including one standard class to do this for you is FAR less cumbersome than faffing around with multiple JADs/JARs IMO.
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;
}
}`
MIDP defines the following constant for the keys of a standard ITU-T keypad: KEY_NUM0, KEY_NUM1, KEY_NUM2, KEY_NUM3, KEY_NUM4, KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8, KEY_NUM9, KEY_POUND, and KEY_STAR. Applications should not rely on the presence of any additional key codes. In particular, upper- and lowercase or characters generated by pressing a key multiple times are not supported by low-level key events. A "name" assigned to the key can be queried using the getKeyName() method.
AFAIR the getKeyName method returned quite the same on most phones so it was quite reliable, but I haven't written anything in j2me since about 2 years ago, so my memory might play tricks.(you have been warned)
Related
I have this event handler in a modelless popup dialog tree control:
void CAssignHistoryDlg::OnTvnSelchangedTreeHistory(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
if (!m_bBuildTreeMode)
{
if ((pNMTreeView->itemOld.hItem == nullptr && !m_bFirstSelChangeEvent) ||
pNMTreeView->itemOld.hItem != nullptr)
{
m_bFirstSelChangeEvent = true;
if (m_treeHistory.GetParentItem(pNMTreeView->itemNew.hItem) == nullptr)
{
// We must update the correct combo
// and associated string (in the SERVMEET_S structure)
if (m_pCombo != nullptr && m_pStrText != nullptr)
{
CString strExtractedName = ExtractName(pNMTreeView->itemNew.hItem);
m_pCombo->SetWindowText(strExtractedName);
*m_pStrText = strExtractedName;
}
GetParent()->PostMessage(UM_SM_EDITOR_SET_MODIFIED, (WPARAM)TRUE);
}
}
}
*pResult = 0;
}
What I don't understand is why once this event is triggered is that it goes in a continuous cycle.
Does anything jump out at you as wrong?
I don't know why the message seemed to be going in a continuous cycle. Maybe it was because I was inserting breakpoints or adding temporary popup message boxes to debug. Either way, I worked out the minor adjustment I needed:
void CAssignHistoryDlg::OnTvnSelchangedTreeHistory(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
if (!m_bBuildTreeMode)
{
if ((pNMTreeView->itemOld.hItem == nullptr && !m_bFirstSelChangeEvent) ||
pNMTreeView->itemOld.hItem != nullptr)
{
m_bFirstSelChangeEvent = true;
if (m_treeHistory.GetParentItem(pNMTreeView->itemNew.hItem) == nullptr)
{
// We must update the correct combo
// and associated string (in the SERVMEET_S structure)
if (m_pCombo != nullptr && m_pStrText != nullptr)
{
CString strExtractedName = ExtractName(pNMTreeView->itemNew.hItem);
m_pCombo->SetWindowText(strExtractedName);
// Bug fix - Only set as modified if the name is different
if(*m_pStrText != strExtractedName)
GetParent()->PostMessage(UM_SM_EDITOR_SET_MODIFIED, (WPARAM)TRUE);
*m_pStrText = strExtractedName;
}
}
}
}
*pResult = 0;
}
As you can see, I have changed how and where I post the UM_SM_EDITOR_SET_MODIFIED message. This causes my application to work correctly. Previously it was always setting it as modified (multiple times). So even if you had just saved the file, it then was marked as modified again. This problem no longer happens.
Am new to MFC, I want to replicate the exact Ctrl+Page Down and Ctrl+Page Up behavior to regular Page Down/Page Up keys without any supporting keys (Ctrl/Shift). I have been trying to clear the focus of item which is getting selected automatically on striking the keys Page Up and Page Down.
I've tried with this code but its not working:
case VK_NEXT: // pagedown
case VK_PRIOR: // pageup
lhItem = GetFocusedItem();
if (IsSelected(lhItem))
{
CTreeCtrl::SetItemState(lhItem, 0, TVIS_SELECTED);
}
break;
Can anyone please help me in solving it
The Code need to written in OnSelChanging & OnSelChanged Event Handler functions
void CTreeCtrl::OnSelchanging(NMHDR *pNMHDR, LRESULT *pResult)
{
HTREEITEM hNew = pNMTreeView->itemNew.hItem;
HTREEITEM hOld = pNMTreeView->itemOld.hItem;
m_bOldItemSelected = hOld && (CTreeCtrl::GetItemState(hOld, UINT(TVIS_SELECTED)) & TVIS_SELECTED);
if (GetSelectedCount() > 1)
{
if (m_bPgUpState || m_bPgDownState)
{
//Check the state of New Item
if ((pNMTreeView->itemNew.state & TVIS_SELECTED))
{
// If the item is selected, so make sure OnSelchanged()
// will "select" it !
m_bNewItemSelected = TRUE;
}
else if (!(pNMTreeView->itemNew.state & TVIS_SELECTED))
{
// The New item is not selected, so make sure OnSelchanged()
// will not "re-select" it !
m_bNewItemSelected = FALSE;
CTreeCtrl::SetItemState(hNew, UINT(~TVIS_SELECTED), UINT(TVIS_SELECTED));
}
}
}
void TreeCtrl::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
HTREEITEM itemNew = pNMTreeView->itemNew.hItem;
HTREEITEM itemOld = pNMTreeView->itemOld.hItem;
if ((m_bPgUpState || m_bPgDownState) && (GetSelectedCount() > 1)
&& (pNMTreeView->itemOld.hItem != NULL || pNMTreeView->itemNew.hItem != NULL))
{
// It had the focus so Keep selection at old item
if (itemOld && m_bOldItemSelected)
{
CTreeCtrl::SetItemState(itemOld, UINT(TVIS_SELECTED), UINT(TVIS_SELECTED));
m_bOldItemSelected = FALSE;
}
else
{
// Do-not select the item if it is not selected
CTreeCtrl::SetItemState(itemOld, UINT(~TVIS_SELECTED), UINT(TVIS_SELECTED));
}
}
In this article you'll find solution for every thing about CTreeCtrl
Full-Featured Tree Control
I wanted to ask,... What would be the most efficient way to detect the Google Voice Search (app id: com.google.android.voicesearch) installation on Android device?
And is there any way of subsequently installing it without taking user to Play Market and back to application?
You can use the below method
private boolean voiceSearchExists() {
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
final List<ResolveInfo> pkgAppsList = getPackageManager().queryIntentActivities( mainIntent, 0);
boolean packageExists = false;
boolean activityExists = false;
for(PackageInfo r:packs){
String pkg = r.applicationInfo.packageName;
if(pkg != null && pkg.equals("com.google.android.googlequicksearchbox")){
packageExists = true;
break;
}
}
if(packageExists == false)
return false;
for(ResolveInfo r:pkgAppsList){
ActivityInfo info = r.activityInfo;
if(info!=null && info.name!=null && info.name.equals("com.google.android.googlequicksearchbox.VoiceSearchActivity")){
activityExists = true;
break;
}
}
return activityExists;
}
I have a tree structure and I want to find all nodes matching a given criteria. Each time I call the find function, it returns next matching node. Children are searched by recursive function call.
For some reason a key comparison of pointers fails for this implementation. Please see the code below, I have pointed out the failing comparison.
HtmlTag* HtmlContent::FindTag(string tagName, string tagParameterContent)
{
if (tagName.empty() && tagParameterContent.empty())
return NULL;
if (this->startTag == NULL)
return NULL;
this->findContinue = this->FindChildren(this->startTag, &tagName, &tagParameterContent);
return this->findContinue;
}
HtmlTag* HtmlContent::FindChildren(HtmlTag* firstTag, string* tagName, string* tagParameterContent)
{
HtmlTag* currentTag = firstTag;
HtmlTag* childrenFound = NULL;
while (currentTag != NULL)
{
if (!tagName->empty() && *tagName == currentTag->tagName)
{
if (tagParameterContent->empty() || currentTag->tagParameters.find(*tagParameterContent, 0) != -1)
{
if (this->findContinue == NULL)
break; // break now when found
else if (this->findContinue == currentTag) // TODO why this fails?
this->findContinue == NULL; // break on next find
}
}
if (currentTag->pFirstChild != NULL)
{
childrenFound = this->FindChildren(currentTag->pFirstChild, tagName, tagParameterContent);
if (childrenFound != NULL)
{
currentTag = childrenFound;
break;
}
}
currentTag = currentTag->pNextSibling;
}
return currentTag;
}
VC++ compiler accepts this code but for some reason I can't put a breakpoint on this comparison. I guess this is optimized out, but why? Why this comparison fails?
I think that you shoud replace == with = in assignment after comparison. Compiler optimalized this whole section because it doesnt do anything useful.
I have a CDialog and I am trying to override PreTranslateMessage like this:
BOOL CWeekendMeetingDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_SETFOCUS)
{
if (::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_PT_CHAIRMAN &&
::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_PT_OPEN_PRAYER &&
::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_WT_CLOSE_PRAYER &&
::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_WEEKEND_HOST &&
::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_WEEKEND_COHOST &&
::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_PT_READER)
{
if (m_gridAssignHist.GetRowCount() != 1)
{
m_gridAssignHist.SetRowCount(1);
}
}
}
return CDialog::PreTranslateMessage(pMsg);
}
It is not working. What I want to do is reset my CGridCtrl row count to 1 when other controls gain focus (eg: edit combo). But if I put a breakpoint inside the first if it is never intercepted.
At the moment the only thing I can think of is to renumber all of the combo IDs on the dialog so they are sequential and then use a command range for OnSetFocus and detect in that handler. But there are also some CEdit controls.
Can't I use PTM though and avoid that? For me it seems it would be the easiest.
Here is the dialog:
At the moment I have 7 combo OnSetFocus handlers. When they fire the display specific assignment history in the grid on the right.
So if the user moves to any other control on the dialog, the assignment history control will not apply. And that was why i wanted to reset the history to just the header row. And I was hoping to do this with PTM.
Child control notifications are received in the parent dialog via WM_COMMAND messages. Overriding MFC dialog's OnCommand catches those notifications (CBN_SETFOCUS, EN_SETFOCUS etc).
void DoSomething()
{ /* ... */ }
BOOL CWeekendMeetingDlg::OnCommand(WPARAM wParam, LPARAM /*unused*/)
{
switch (HIWORD(wParam))
{
case CBN_SETFOCUS:
switch (LOWORD(wParam))
{
case IDC_COMBO_PT_CHAIRMAN:
// ...more combobox IDCs
DoSomething();
break; // or return TRUE to bypass any message map handlers
}
break;
case EN_SETFOCUS:
switch (LOWORD(wParam))
{
case IDC_EDIT_WHATEVER:
// ...more editbox IDCs
DoSomething();
break;
}
break;
// ...more notification codes
}
return CDialog::OnCommand(wParam, lParam);
}
I have added a 2 second timer to the dialog:
void CWeekendMeetingDlg::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == 1)
{
CWnd* pControl = GetFocus();
if (pControl != nullptr)
{
CWnd* pParent = pControl->GetParent();
if (pParent != nullptr)
{
int iCtrlID = pParent->GetDlgCtrlID();
if (iCtrlID != IDC_COMBO_PT_CHAIRMAN &&
iCtrlID != IDC_COMBO_PT_OPEN_PRAYER &&
iCtrlID != IDC_COMBO_WT_CLOSE_PRAYER &&
iCtrlID != IDC_COMBO_WEEKEND_HOST &&
iCtrlID != IDC_COMBO_WEEKEND_COHOST &&
iCtrlID != IDC_COMBO_PT_READER)
{
if (m_gridAssignHist.GetRowCount() != 1)
{
m_gridAssignHist.SetRowCount(1);
UpdateData(TRUE);
m_strAssignHistLabel = _T("Assignment:");
UpdateData(FALSE);
}
}
}
}
}
CDialog::OnTimer(nIDEvent);
}
This seems to work fine.