I'm try make program to open other apps, to open this program i wan't use keybinds and for hide too.
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (Dark == 0)
{
if (e.Key == Key.LeftCtrl)
{
if (e.Key == Key.LWin)
{
this.Topmost = true;
Dark = 1;
}
}
}
else if(Dark == 1)
{
if (e.Key == Key.LeftCtrl)
{
if (e.Key == Key.LWin)
{
this.Topmost = false;
Dark = 0;
WindowState = WindowState.Minimized;
}
}
else if (e.Key == Key.Escape)
{
this.Topmost = false;
Dark = 0;
WindowState = WindowState.Minimized;
}
}
Outside the program, keybinds do not work at all, but if the program is open, then they work, why?
you can refer to my class, used to global hook key event
https://github.com/nhochjkaru/JEOrbwalk/blob/master/UserActivityHook.cs
declare: UserActivityHook actHook;
Main function:
actHook = new UserActivityHook();
actHook.KeyDown += new KeyEventHandler(Window_KeyDown);
Related
I am working on my project and i need to add an action that is only performed while left CTRL is pressed
This is my code:
private void Izrada_kartice_KeyDown(object sender,KeyEventArgs e)
{
if(e.Control)
{
promijeni_veličinu_naslov = true;
this.BackColor = Color.Red;
}
}
private void Izrada_kartice_KeyUp(object sender, KeyEventArgs e)
{
if (e.Control)
{
promijeni_veličinu_naslov = false;
this.BackColor = Color.Green;
}
}
Now when I run this code and press CTRL all is OK, but when I release CTRL
nothing happened.
I was try this:
private void Izrada_kartice_KeyDown(object sender,KeyEventArgs e)
{
if(e.KeyCode == Keys.A)
{
promijeni_veličinu_naslov = true;
this.BackColor = Color.Red;
}
}
private void Izrada_kartice_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.A)
{
promijeni_veličinu_naslov = false;
this.BackColor = Color.Green;
}
}
This works fine but I need do this with CTRL
The KeyUp event will fire when you release Ctrl, but the e.Control boolean is not set to True because Ctrl is no longer being held down.
In short: Don't detect the release of Ctrl by inspecting e.Control, inspect the e.KeyCode instead; it will be Keys.ControlKey
if(e.KeyCode == Keys.ControlKey)
{
...
}
On your second code, replace Keys.A with Keys.ControlKey like this:
private void Izrada_kartice_KeyDown(object
sender,KeyEventArgs e)
{
if(e.KeyCode == Keys.ControlKey)
{
promijeni_veličinu_naslov = true;
this.BackColor = Color.Red;
}
}
private void Izrada_kartice_KeyUp(object sender,
KeyEventArgs e)
{
if(e.KeyCode == Keys.ControlKey)
{
promijeni_veličinu_naslov = false;
this.BackColor = Color.Green;
}
}
In my mini app in C# I have a button that I want to enable and disable it by right click on it. i.e. when button is enabled by right click it turn to disable and when it is disable, right click on it change it's status to enable. Disabling of enabled button is easy and straightforward but enabling it by right click on it, is not possible; because the button is disabled and not event is sent to emaciate code.
How Can I do?
The mouse events of a disabled control are passed down to its Parent.
You can catch them there and test if the cursor is on the button.
Example:
if (button1.Bounds.Contains(e.Location)) button1.Enabled = true;
If you have several buttons you need to test them all..:
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
foreach (Control ctl in panel1.Controls)
{
if (ctl is Button && ctl.Bounds.Contains(e.Location))
ctl.Enabled = true;
}
}
If only the right mouse button is supposed to enable, add a test for it, maybe like so:
if (e.Button.HasFlag(MouseButtons.Right) &&
ctl is Button && ctl.Bounds.Contains(e.Location))
Right-clicking to enable and disable a button is not standard or recommended button behavior, so the API completely leaves out an easy implementation for it. Once the button is disabled, it no longer captures mouse events, but the form does.
public partial class Form1 : Form
{
private Boolean _button1IsRightClicked;
public Form1()
{
InitializeComponent();
_button1IsRightClicked = false;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hi");
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.X >= button1.Location.X && e.X <= (button1.Location.X + button1.Width) &&
e.Y >= button1.Location.Y && e.Y <= (button1.Location.Y + button1.Height))
{
_button1IsRightClicked = true;
}
else
{
_button1IsRightClicked = false;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.X >= button1.Location.X && e.X <= (button1.Location.X + button1.Width) &&
e.Y >= button1.Location.Y && e.Y <= (button1.Location.Y + button1.Height))
{
_button1IsRightClicked = false;
enableDisableButton1(button1.Enabled);
}
else
{
_button1IsRightClicked = false;
}
}
private void enableDisableButton1(Boolean isEnabled)
{
if (isEnabled)
{
button1.Enabled = false;
isEnabled = false;
}else
{
button1.Enabled = true;
isEnabled = true;
}
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
_button1IsRightClicked = true;
}
else
{
_button1IsRightClicked = false;
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && _button1IsRightClicked == true)
{
_button1IsRightClicked = false;
enableDisableButton1(button1.Enabled);
}
else
{
_button1IsRightClicked = false;
}
}
}
I've started creating a game and i ran into a problem:
I cant place check boxes because it somehow stops the "players" movement.
I'm moving my "player" using arrow keys.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Left)
{
left = true;
}
if (e.KeyCode == Keys.Right)
{
right = true;
}
if (e.KeyCode == Keys.Up)
{
up = true;
}
if (e.KeyCode == Keys.Down)
{
down = true;
}
}
And to reset the controls:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
left = false;
}
if (e.KeyCode == Keys.Right)
{
right = false;
}
if (e.KeyCode == Keys.Up)
{
up = false;
}
if (e.KeyCode == Keys.Down)
{
down = false;
}
}
So how can i place a check box and still control my "player"?
The game working without the check box:
https://i.imgur.com/oJmn2uD.gifv
Then i put the check box and not being able to move the "player":
https://i.imgur.com/53WOGjW.gifv
What are other ways to add a checkbox sort of control that i could use for settings tab that would work?
The answer was that i cant use arrows keys because they act as input keys so i replaced them with WASD and it works like a charm!
I have a picturebox that I want to relocate when a key is pressed.
I'm making a game of pong. With the code below I want to make the paddle of player 1, which is a picturebox that should move up and down while a key is pressed. The following code doesn't seem to work:
public partial class Form1 : Form
{
bool upPressed, downPressed;
public Form1()
{
InitializeComponent();
}
private void PongTimer_Tick(object sender, EventArgs e)
{
if(upPressed){
paddlePlayer1.Location = new Point(paddlePlayer1.Location.X, paddlePlayer1.Location.Y + 5);
}
if (downPressed)
{
MessageBox.Show("Numlock");
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Up){
upPressed = true;
}
if (e.KeyCode == Keys.NumLock)
{
downPressed = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Up){
upPressed = false;
}
}
}
I used the Numlock to see if the problem was in the picture, this isn't the case. I've put the KeyPreview propery to true but this didnt solve it either. The timer does work properly.
I hope somebody here can tell me what I'm doing wrong.
This is a code example to move a PictureBox:
Drag/drop a PictureBox on a form (in this case Form1).
Change the Background color of the PictureBox to e.g. red. (This way it is visible during runtime).
Implement the KeyDown event of Form1 (in this case Form1_KeyDown).
Run the application.
Start pressing an arrow key and the PictureBox will move.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.Right) { x += 1; }
else if (e.KeyCode == Keys.Left) { x -= 1; }
else if (e.KeyCode == Keys.Up) { y -= 1; }
else if (e.KeyCode == Keys.Down) { y += 1; }
pictureBox1.Location = new Point(x, y);
}
I've been breaking my head over a problem I have (I just started coding in C#)
I have a Main Screen (Hereafter reffered to as Form 1) and a Video Options Form (hereafter reffered to as Form 2) (Picture Included). Now, when f.e. I change the Radiobuttons in Form 2 to "Windowed" and Select a Resolution, I want some options to change in Form 1.
This is as far as I got so far, it goes to the Form 1 code, but says I can't change anything there.
Code Snippet Main Form
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void btnVideo_Click(object sender, EventArgs e)
{
Visual_Options options = new Visual_Options();
options.Show();
}
Code Snippet Options Form
public partial class Visual_Options : Form
{
frmMain Main;
public Visual_Options()
{
InitializeComponent();
}
private void Visual_Options_Load(object sender, EventArgs e)
{
switch (Main.FormBorderStyle) //Check the Borderstyle of frmMain with Switch to determine current state
{
case FormBorderStyle.None: // if BorderStyle of frmMain = "None"
if (Main.WindowState == FormWindowState.Maximized) //Check if frmMain = Maximizes
{
rbFullscrn.Checked = true;
}
else
{
rbBorderless.Checked = true;
};
break;
case FormBorderStyle.Fixed3D:
rbWindow.Checked = true;
break;
}
switch (Main.Width) //Check Width to determine current value
{
case 800:
rb8x6.Checked = true;
break;
case 1024:
rb10x7.Checked = true;
break;
case 1280:
rb12x7.Checked = true;
break;
}
}
private void btnAccept_Click(object sender, EventArgs e)
{
if (rbFullscrn.Checked == true)
{
Main.WindowState = FormWindowState.Maximized;
Main.FormBorderStyle = FormBorderStyle.None;
}
else if (rbBorderless.Checked == true && rb8x6.Checked == true)
{
Main.WindowState = FormWindowState.Normal;
Main.FormBorderStyle = FormBorderStyle.None;
Main.Height = 600;
Main.Width = 800;
}
else if (rbBorderless.Checked == true && rb10x7.Checked == true)
{
Main.WindowState = FormWindowState.Normal;
Main.FormBorderStyle = FormBorderStyle.None;
Main.Height = 768;
Main.Width = 1024;
}
else if (rbBorderless.Checked == true && rb12x7.Checked == true)
{
Main.WindowState = FormWindowState.Normal;
Main.FormBorderStyle = FormBorderStyle.None;
Main.Height = 720;
Main.Width = 1280;
}
else if (rbWindow.Checked == true && rb8x6.Checked == true)
{
Main.WindowState = FormWindowState.Normal;
Main.FormBorderStyle = FormBorderStyle.Fixed3D;
Main.Height = 600;
Main.Width = 800;
}
else if (rbWindow.Checked == true && rb10x7.Checked == true)
{
Main.WindowState = FormWindowState.Normal;
Main.FormBorderStyle = FormBorderStyle.Fixed3D;
Main.Height = 768;
Main.Width = 1024;
}
else if (rbWindow.Checked == true && rb12x7.Checked == true)
{
Main.WindowState = FormWindowState.Normal;
Main.FormBorderStyle = FormBorderStyle.Fixed3D;
Main.Height = 720;
Main.Width = 1280;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
I've looked on the internet for a bit now, and, I found a few solutions, but, from the few solutions I found, none seemed to work so far.
Ty in advance,
Me ^_^
If you use ShowDialog, it will open your options Form as a Modal Dialog, that way once you make changes you can check the DialogResult and read a Public Property from your Option Form in your Main Form, using it to set your size. Otherwise you could subscribe to the Options Form's Closed Event and use that event to set your Forms Size.
Fist Option:
private void button1_Click(object sender, EventArgs e)
{
Visual_Options options = new Visual_Options();
if ( options.ShowDialog() == DialogResult.OK)
this.Size = options.getFormSize; //This is a public property returning a size
}
2nd Option using same property:
private void button1_Click(object sender, EventArgs e)
{
Visual_Options options = new Visual_Options();
options.FormClosed+=new FormClosedEventHandler(options_FormClosed);
options.Show();
}
void options_FormClosed(object sender, FormClosedEventArgs e)
{
this.Size = ((Visual_Options)sender).getFormSize;
((Visual_Options)sender).FormClosed -= new FormClosedEventHandler(options_FormClosed); //Remove handler to prevent leaks
}
You might want to do this:
options.Show(this); passing the this reference will make "options" form as the child of your mainform.
Then frmMain Main can be set as:
frmMain Main = (frmMain)this.Owner;
After this, you can alter any and all properties of your main form.