I am developing an application that requires me to override keypressed and released methods in lwuit so as to map specific functons into gamekeys.
The gamekeys work fine when I do this but I'm having trouble adding more than two commands to application. Normally actionlistener would automatically handle the mapping of multiple commands but it does not.
Is it possible to map more than one command to a softkey (like with keycode -7) and have an if statement to dynamically check which command is pressed.
public void keyReleased(int keyCode) {
switch (keyCode) {
case -6: // left cmd key
function a();
return;
case -7: // right cmd key
//Need this to handle more than one command function
return;
}
//function to handle gamekeys
}
Hope I am clear enough with my issue. Please help
In my opinion what you wanna do is not possible. There are always a cancel command in a softkey and the menu (if there are more than one command added) in the other softkey.
To get the correct keyCode, to set another funtionallity to the softkey you should get the correct keyCode. Make a System.out.println("keycode " + keyCode); before the first line in the method keyReleased
You need to replace the MenuBar class if you want to do custom key/menu handling. Just subclass the MenuBar and define your new class within the LookAndFeel.
Related
I am trying to set up a MFC C++ App in Visual Studio 2019 such that modifies the user's text as they are typing.
Current layout is 2 radio buttons,
ID= rdbOn (set to Group = True, with Value int variable m_isOn = 1)
ID= rdbOff, m_isOn value would be = 0
and 1 Edit Control,
ID= txtInputBox, with Value CString variable m_inputString
Currently, for testing I can see how it would work for a button on click, it would take something like the following and just SetDlgItemText of the result. But that would be after they have typed, not WHILE they are typing.
void Onsomebtnclick()
{
//convert CString to String of m_inputString
//do some string manipulation
//convert back to CString
//SetDlgItemText(txtInputBox, result)
}
Update:
got EN_CHANGE to work
I was able to get EN_CHANGE working with the flag suggestion from user #GoGoWorx. However, now I just have a slight problem that the cursor is back to the beginning of the edit control txtInput.
I'm reading about using a CEdit::SetSel but don't know how to use that directly in my code. I tried
CEdit control MFC, placing cursor to end of string after SetWindowText
someDlg::someFunction()
{
//some logic stuff to get a result string
SetDlgItemText(txtInputBox, result);
//need it to set the cursor to the end
//I tried these, but it didn't recognize (expression must have class type?)
//txtInputBox.SetSel(0, -1);
//txtInputBox.SetSel(-1);
}
It sounds like you need to use the ON_EN_CHANGE message-map notification (called after the control has been updated due to typing or pasting for example)
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
ON_EN_CHANGE(IDC_EDIT_CONTROL, &CMyDialog::OnEnChangeEditControl)
END_MESSAGE_MAP()
void CMyDialog::OnEnChangeEditControl()
{
// Copy or call your Onsomebtnclick() here
}
I'm not sure what you're using for the numeric identifier for the edit control, since these are typically upper case defines - replace IDC_EDIT_CONTROL above with your define (possibly txtInputBox, but again, these are normally upper case, so I'm not sure).
Also change CMyDialog for the name of your dialog class too.
Note that we're using the ON_EN_CHANGE message-map handler here instead of the ON_EN_UPDATE, since the ON_EN_CHANGE message is sent after the control has been updated, whereas ON_EN_UPDATE is called just before it's updated.
The message-map handlers are described in the Remarks section of the CEdit control documentation: https://learn.microsoft.com/en-us/cpp/mfc/reference/cedit-class?view=msvc-160
Regarding your concern about modifying things as the user types - this should be fine, since every change (keystroke or paste from clipboard, etc.) should trigger this handler to be called, where you can change whatever you need. Just be sure that when you're updating the control, you don't trigger the ON_EN_CHANGE again and end up in a recursive 'change' loop.
You might be able to do this with some sort of flag to indicate you're the one updating the control, as opposed to the user, however it's probably better to subclass the CEdit control to do what you're wanting. There are a few examples out there of how to do this (it's not as difficult as it might sound), for example:
https://www.codeproject.com/Articles/27376/Avoiding-EN-CHANGE-notifications
i want to update the text field value when i was changed.for this i use action listener because every updation time one trigger was fired in my code so,that's the reason i used action listener instead of data changed listener.for data changed listener on key pressed the listener is called updation done but my requirement is after completion of entering the data on text field only the listener is called.but it is not calling properly.
please find the below code,
agencyWorker.addActionListener(createAgencyActionListener(agencyWorker,eventPostchedules.getSerialId()));
private ActionListener createAgencyActionListener(final TextField searchField, String serialId){
return new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
String agencyText = searchField.getText();
searchField.setPreferredW(110);
roasterDao = RoasterDao.getInstance();
roasterDao.updateEventPostScheduleAgency(agencyText, serialId, "supervisor");
}
};
}
thanks in advance.
I just tested this on my iPad and action listener was invoked as expected. Check that you don't have a different error that is causing this e.g. networking error related to https change in iOS.
FYI on a side note, it's really bad to write this:
searchField.setPreferredW(110);
You are limiting the size of the field based in pixels which is rarely the right thing to do. The method is deprecated for a reason...
I have an EditText, and am attempting to listen for keystrokes with setOnKeyListener, so that when I detect an Enter keypress, it performs some action. It works fine with hard keyboards and emulators, but not on my Galaxy Nexus.
The documentation for setOnKeyListener says "Key presses in software input methods will generally not trigger the methods of this listener", which explains why not.
I don't want to explicitly add a "Done" button onscreen as in the Building Your First App tutorial.
I've now set it up to use the android:inputType="text" property in the layout xml, and it seems to have the desired effect. Is this considered the correct approach? Is it documented anywhere? Will it work on all devices? Is there a better way?
Here is the code I'm using in my Activity:
myEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Process the entered text here
myEditText.setText("");
return true;
}
return false;
}
}
This is based on the ToDo List Example in Chapter 4 of Professional Android 4 Application Development.
It appears that android:inputType="text" is indeed the correct approach.
In our RCP application, we need to resort to using a global key event handler (via Display.addFilter()) for more advanced key event handling/routing irrespective of current focus. We need to be able to determine if a dialog box is currently open for some of the routing logic.
Seems like a fairly trivial question but I keep hitting dead ends going off Widget hierarchy, Shells, WindowManagers.
I am looking for a robust solution that would not require any extra work on the part of Dialog implementers or client code that uses standard framework dialogs.
In the example below, shell is a defined Shell in the scope. You could modify the code to compare activeShell with a list of Shells.
shell.getDisplay().addFilter(SWT.KeyDown, new Listener() {
public void handleEvent(final Event event) {
if (shell.isDisposed()) {
return;
}
final Shell activeShell = shell.getDisplay().getActiveShell();
if (activeShell != null && activeShell.equals(shell)) {
if (event.stateMask == SWT.MOD1 && event.character == 'w') {
shell.dispose();
}
}
}
});
This example code will close shell when ⌘+W is pressed on Mac.
How can I stop mfc dialog application closing by pressing ESC (Escape key).
After executing my application if I press ESC key then the window is closed.
How can this be stopped?
I am using VC++ 6.0.
You can override the OnCancel event and only move forward with the OnCancel call if IDCANCEL is the focused item.
void CMyDialog::OnCancel(void)
{
if(GetDlgItem(IDCANCEL) == GetFocus())
{
CDialog::OnCancel();
return;
}
}
There are different ways to do this. You can:
Create an OnCancel Handler and do whatever you want with the Cancel notification
You can Handle OnClose Event and do whatever you want.
You can override PreTranslateMessage and check Esc key there and do whatever you want.
Check this for code examples.
For a PreTranslateMessage example, see this
Override OnCancel and don't call the base class implementation.
Don't go near OnClose unless you know what you're doing, you risk breaking the behaviour for Alt-F4 and the X button.
I've always regarded PreTranslateMessage for things like this as using a thermo-nuclear weapon to crack a nut, but if it floats your boat...
Assuming we're dealing with a top-level window implemented as a CDialog subclass here, that window can receive two "kinds" of close events:
Application close events (WM_SYSCOMMAND with an ID of SC_CLOSE)
Window close events (WM_COMMAND with an ID of IDOK or IDCANCEL)
MFC, however, effectively routes the former class of events through CDialog::OnCancel by default when they are sent to a dialog, which means that overriding OnCancel also breaks Alt-F4 and the X button. This means that in order to distinguish between the two, you need to handle the former events in OnSysCommand while using overrides of OnOK and OnCancel to handle the latter.
The resulting code looks something like this:
class CTopLevelDlg: public CDialog
{
afx_win void OnSysCommand(UINT id, LPARAM lparam) override
{
if (id == SC_CLOSE)
CDialog::OnCancel();
}
void OnOK() override {}
void OnCancel() override {}
};