Disable keyboard "return" key in SwiftUI - swiftui

Now in SwiftUI, keyboard automatically dismisses when we hit the return key on the keyboard.
In my case, I want the user not to accidentally dismiss the keyboard by hitting the return key.
There's no any keyboard type in .keyboardType(.****) without the return key
So, is there any way to disable the return key from dismissing the keyboard or is there any way to remove the return key from the keyboard?

.keyboardType(.twitter) is what you are looking for.

Related

How to stop keyboard input for Edit Control in a Win32 MFC application after Enter key was pressed

I'd like my Edit Control to stop accepting keyboard input after I press the enter key. I am detecting the Enter key being pressed.
Currently, I don't have a handle to the Edit Control, so if that's required for this operation, please let me kow how to do that as well.
Although this is clearly more of a workaround, my issue was resolved by using the GotoDlgCtrl function to focus on a different element (apply button in this case). This stops the CEdit in question from taking further keyboard input.
For getting keyboard input, DirectX::Keyboard can be used.
if (DirectX::Keyboard::Get().Enter)
{
CButton* button = (CButton*)GetDlgItem(ID_BUTTON_APPLY);
GotoDlgCtrl(button);
}

How to prevent ENTER Key on Buttons

I have a CFormView Dialog with Buttons to send commands for a hardware I/O.
I the user accidentally click the enter key, the command is executed. (the last button one, that has the focus).
How do you solve this in the correct way?
The way ist to use PreTranslateMessage. CHeck for WM_KEYDOWN and VK_ENTER. Ignore it or do whatever you want.
Background: CFormView::PreTranslateMessage later calls PreTranslateInput and this finally calls IsDialogMessage and this functions translates the Enter key to execute the default dialog button.

MessageBox - return value when box has no Cancel button

according to the MessageBox documentation:
Return value
Type: int
If a message box has a Cancel button, the function returns
the IDCANCEL value if either the ESC key is pressed or the Cancel
button is selected. If the messagebox has no Cancel button, pressing
ESC has no effect.
What if I wish to have a a box that has no Cancel button but I want to distinguish between OK and close/ESC?
MessageBox() does not support the behavior you are looking for. You would have to hook the dialog directly, using SetWindowsHookEx() or SetWinEventHook(), in order to detect it being closed.
Use TaskDialogIndirect() instead. It has a TDF_ALLOW_DIALOG_CANCELLATION flag:
Indicates that the dialog should be able to be closed using Alt-F4, Escape, and the title bar's close button even if no cancel button is specified in either the dwCommonButtons or pButtons members.
All of those conditions will return IDCANCEL.
What if I wish to have a a box that has no Cancel button but I want to distinguish between OK and close/ESC?
Standard dialogs don't offer such behaviour because it is very poor design. As a point of principle, a GUI should allow actions to be performed by mouse or keyboard. A hidden action only accessible by keyboard is a sign of poor design.
If you really want to make such a dialog, you'll have to implement it yourself. However, you should not. Present the dialog with OK and Cancel buttons.

How to use CDialog::SetDefId with a non button control?

I've come across CDialog::SetDefId and while it is pretty easy and clear that it is for "pushbuttons" I wanted to use this functionality with a non button control.I understand that you have to press Enter or Return to make the dialog use that ID
If I set nID to 0 in the CDialog::OnInitDialog and have no default button setted the dialog will default to CDialog::OnOk, If I do have a default button setted, the dialog will push that button as expected.The thing is that I want to make this work for a non button control, so if I set nID to a control that is not a pushbutton, the dialog will do nothing even If I set the message handler for the keydown event or NM_RETURN, it doesn't matter if the control has focus or not, the dialog will still do nothing if I press Enter or Return.How can I make the control be the default control without using stuff like PreTranslateMessage? and which message is sent to the control?thanks in advance
Yes, it only works with buttons but you can use SetFocus to change focus to any other control:
BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
GetDlgItem(IDC_CHECK1)->SetFocus();
return 0; // return TRUE unless you set the focus to a control
}
In this example the OK button may still be the default button. Enter key will go to default button, probably IDOK. But space key it will change the check box IDC_CHECK1.
There has to be a default button. If you don't want one, then add a fake button, lets say IDC_BUTTON1, and make it the default button and not visible, then you don't see a default button (you can still add IDC_BUTTON1 to message map and decide what to do with Enter key)

Allowing Enter in hotkey control

I'm using the standard hotkey control to allow user to specify a key combination for a certain action. However, it doesn't allow using the Return (Enter) key. Is there a way to allow it? My action is sort-of chat reply, so having a hotkey like Alt+Enter makes the most sense.
The only way would be to sub-class the control, since by default it rejects keys like VK_RETURN.
Your sub-class would probably need to do two things:
Handle the WM_GETDLGCODE message and return DLGC_WANTMESSAGE when the message indicates the enter key has been pressed
Handle the WM_KEYDOWN message when return is pressed, and manually set the hotkey in the control by sending it a HKM_SETHOTKEY message.