Answered Run program in the background and enable buttons with hotkey

Reox

New member
Joined
May 12, 2020
Messages
2
Programming Experience
1-3
So, after a long time I installed Visual Studio to make a program. Something I couldn't find on the internet. What I want to do is, I want to run this program in the background and when I press a key combination, my Button starts to enable. Program works but I have no idea how I can make it run in the background. I searched on the internet and tried few things and it didn't work well.

This is my code:

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InternetTry
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.Control == true && e.KeyCode == Keys.F1)
            {
                button1.PerformClick();
            }
            if (e.Control == true && e.KeyCode == Keys.F2)
            {
                button2.PerformClick();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("ipconfig", "/renew"); //For enabling internet
        }

        private void button2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("ipconfig", "/release"); //For disabling internet
        }
    }
}
 
If you want to be able to react to keyboard input when your form does not have focus then you will need to look at using the RegisterHotKey Windows API function. There are lots of examples of its use on the web.
 
As a quick aside, you should not be using PerformClick() to get the action performed by an event handler to run. That is a kludge. The correct thing to do is factor out the code that you want to call:

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
    
namespace InternetTry
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true && e.KeyCode == Keys.F1)
            {
                EnableInternet();
            }
            if (e.Control == true && e.KeyCode == Keys.F2)
            {
                DisableInternet();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            EnableInternet();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DisableInternet();
        }
        
        void EnableInternet()
        {
            Process.Start("ipconfig", "/renew");
        }
        
        void DisableInternet()
        {
            Process.Start("ipconfig", "/release");
        }
    }
}
 
IPConfig /release is not shutting down the internets LAN. Instead it instructs the server/network to release the lease on the ip which sets its status as available for other servers on the same network to be able to claim this IP. It doesn't actually disable the local area network. Just thought I'd point that out and make sure you are aware of that in case this isn't what you want.
 
Yup, I'm aware. I was just practicing the best practice of instead of using comments, actually naming methods by what the comments say. In this case, the OP's comments said that's what the line was doing.
 
Hey, so I didn't get far with that (watched few videos RegisterHotKey but didn't get far), so I googled even more and at the end I found some help, so this can be closed.

Thanks for all the replies!

btw: @Sheepings , my goal was to shut down my own connection when I press the key and enable it. I did it because I was bored :p
 
If you post the answer you found, I can mark the topic as solved.

Sharing your answers helps future readers who are trying to do what you are doing now.
 
Back
Top Bottom