How to detect key presses in C# - keypress

So I started programming today, and I want to make a small platforming stage in C#. I would like to know, how can I detect for example a left arrow key press. I can't find information that summarize my exact problem.

private void Action_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
// do something
}
}
Edit: Under Solution Explorer select some Form where you want to control key press feature. With right mouse click select View Designer. Next, under Properties section click on Events icon. Finally, find KeyDown Event and press Enter Key, it will result with a function which will handle key presses...

Related

wxWidgets can't receive Right mouse click

I'm trying to add a context menu to a grid control in wxWidgets 2.9.4 on Windows 10, and while I can get the context menu key to work right clicking doesn't. Right now I have the following in the header
void handle_contextMenu(wxContextMenuEvent& event);
void handle_rightButton(wxMouseEvent& event);
and in the constructor
Bind(wxEVT_CONTEXT_MENU, &DataGrid::handle_contextMenu, this);
Bind(wxEVT_RIGHT_UP, &DataGrid::handle_rightButton, this);
neither work for the right mouse button.
The reason you can't bind to these events on wxGrid itself is that it's a composite window as explained in the "Accessors for component windows" section of the documentation. So to make this code work you need to call GetGridWindow()->Bind(...), for example.
Alternatively, you could, and probably should, if they're enough, use the higher level events such as the already mentioned wxEVT_GRID_CELL_RIGHT_CLICK.

UI Button - activate buttons with keyboard input

this is probably dead easy but I can't find a solution. I made a dialogue system and have a UI-button to click when the player should display a sentence next.
The issue is that the button is only triggered onMouseclick and I would like to change the input button to Enter. Would anyone know how to go about this?
If you need to determine if the button is selected first or not, I suggest you take a look at this page: https://docs.unity3d.com/ScriptReference/UI.Selectable.IsHighlighted.html
If you don't want pressing the button to have any functionality, you just wouldn't link it to any functions.
Working code might look something like this:
public class selectableExample : Selectable{
BaseEventData _event;
void Update()
{
if (IsHighlighted(_event) == true)
{
if (Input.GetKeyDown("enter")){
print("replace me with working function"); // whatever you want to have happen on button press
}
}
}
}
You simply attach this to your button and it should respond the same as being pressed. To be honest, it hardly seems like you actually need a button at all for this though, you'd probably be fine with just a label telling the player to press "Enter" and then simply checking for that input.
You can use the Event Trigger component to use one of the many event types. Select Submit (this is set to enter and return in the input settings at edit>project settings>input by default).
Don't set anything in the OnClick event.
The only thing needed now is to actively highlight the button from somewhere with ReferenceToButton.Select().

Xamarin.Forms Entry Focus when Unfocused

I need it so that when the Entry loses focus, the focus returns to the Entry (therefore locking the focus on an Entry).
I did the following code:
myEntry.Unfocused += (object sender, FocusEventArgs e) => {
if (!e.IsFocused)
{
((Entry)sender).Focus();
}
};
It works, but the keyboard stops working - I can't write anything.
Is this a bug? Can someone help me?
I found that this is a bug xamarin.forms 2.0.0.0. Updated and solved the problem.
I know on android if you click on an Entry and give it focus, the keyboard will appear, and then if you click another element, the Entry will lose focus and the keyboard will go away. So by refocussing on the Entry when it loses focus, you might be forcing the keyboard to be minimized. This is a total guess though.
What you might be able to do is set the most parent element's InputTransparent property to true and then only set the Entry.InputTransparent property to false so that the user is unable to click anything but the Entry.
If that does not work, then you could also try adding a transparent ContentView that covers everything except the Entry and set the ContentView.InputTransparent to true which would have the same effect.

How to show a dialog from another dialog?

I am newbie to MFC. I have a native C++ MFC app. I want to show a dialog from main dialog. In the main dialog I am having three button (Back, Next, Cancel) respectively.
On the Next button click event I am calling DoModal to show another dialog by hiding the main dialog as follows,
void CFirstPage::OnBnNextButton()
{
::ShowWindow(this->GetSafeHwnd(),SW_HIDE);
CSecondPage secondDlg;
secondDlg.DoModal();
}
void CSecondPage::OnBnBackBtnClicked()
{
::ShowWindow(this->GetSafeHwnd(),SW_HIDE);
CFirstPage FirstPage;
FirstPage.DoModal();
}
After executing this code snippet, the main dialog got hidden and even the application icon also disappears from the taskbar and again appears when the other dialog pops up.
(Basically I am having the same icon for both the dialogs, the icon should not get disappeared and appear again. It has to remain same without appearing and disappearing .)
How can show the icon in the taskbar without any flickering effect?
During traversing from back to next in middle I clicked cancel and the Cancel event is handled as follows,
void CFirstPage::OnCancel()
{
CDialog::EndDialog(TRUE);//For closing the dialog.
}
void CSecondPage::OnCancel()
{
CDialog::EndDialog(TRUE);//For closing the dialog.
}
Steps1:Click Next in the main dialog
Step2: Click Cancel in the second page
Now the application closes. But still instance is active in the "TaskManager". As per my understanding no instance should be alive once windows is closed ?
I suspect as the first dialog is only hidden not ended that instance is still existing in the TaskManager. Is this understanding correct?
How can I resolve this issue?
Can anyone kindly help me to resolve this issue.
As said by Iinspectable property sheets are best suited for your your problem statement.A very good example on how to use CPropertysheets can be found in codeproject
CProperty sheet example
Probably your main windows is still hidden after you end dialog with second page. Ending dialog of CSecondPage does not close application only closes active CSecondPage dialog.
Also OnCancel/OnOK does not need to be overriden if you just EndDialog with it. There is default behaviour implemented in OnCancel, which will close the dialog.
After secondPage.DoModal() show your main dialog again, or close it if that is the behaviour you want to achieve.
FirstPage isn't the original first dialog now, so you should store the first dialog object by yourself. You can do that like this:
void CFirstPage::OnBnNextButton()
{
::ShowWindow(this->GetSafeHwnd(),SW_HIDE);
CSecondPage secondDlg;
secondDlg.setFirstDialog(this); //customer function to store first dialig object
secondDlg.DoModal();
}
void CSecondPage::OnBnBackBtnClicked()
{
::ShowWindow(this->GetSafeHwnd(),SW_HIDE);
::ShowWindow(m_firstDialog->GetSafeHwnd(), SW_SHOW);
}

Hide touch keyboard

Is it possible to not show system keyboard, when you tap textbox? Ive created custom keyboard and can work with textblock only, because of this i cant delete just parts of sentence.
If you set IsReadOnly to true then the user can still select the text in a TextBox to copy paste and the OS doesn't show the software input keyboard when selected. You can still alter the contents of the TextBox through code though. Eg;
<TextBox x:Name="ExampleTextBox"
IsReadOnly="True"
Text="Initial Content"
GotFocus="ExampleTextBox_GotFocus"
/>
And in your code behind;
private void ExampleTextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e) {
ExampleTextBox.Text += " ... focused!";
}
Would prevent the user from entering text via the software keyboard but will append "... focused" everytime they give focus to the TextBox. Contrived example, but you get the idea.
The only other thing I'd suggest is to re-style the TextBox. By default when IsReadOnly is set the TextBox will provide visual cues that it can't be modified by the user. Which isn't the case here.
if the user touches the keyboard, the keyboard will get focus.
the only option you as a developer have is to catch it and call this.focus moving focus away from textbox.
This will however mean that there will be a flicker where default keyboard pops up and is hidden.
I know this because i have a keyboard app. There is no other way.

Resources