How to simulate the event of typing the% symbol (%) when pressing Shift + 5?

4

I was developing a calculator in C #, and I started to do the activation part of the buttons through the keyboard.

if (e.KeyCode == Keys.Add) 
  {
    btnsum.PerformClick();
  } 

Using the above code, I've already been able to do most of the button activation, but then my question popped up.

Is there a way to do the % (percentage) button activation by pressing Shift together with 5 ?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculadora
{
    public partial class frmCalculadora : Form
    {
        public frmCalculadora()
        {
            InitializeComponent();
        }

        public string n1 = "", n2 = "";
        double memory = 0;
        public bool estado = false;

        class Calc
        {
            public string Op = "";

            public string Igual(string strN1, string strN2)
            {
                double n1 = Convert.ToDouble(strN1);
                double n2 = Convert.ToDouble(strN2);
                double resul = 0;

                switch (Op)
                {
                    case "/":
                        resul = n1 / n2;
                        break;
                    case "*":
                        resul = n1 * n2;
                        break;
                    case "-":
                        resul = n1 - n2;
                        break;
                    case "+":
                        resul = n1 + n2;
                        break;
                    case "mod":
                        resul = n1 % n2;
                        break;
                    case "log":
                        resul = Math.Pow(n1, n2);
                        break;
                }
                return Convert.ToString(resul);
            }
        }
        Calc c = new Calc();

        private void btn0_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "0";
                estado = false;
            }
            else
            {
                txtVisor.Text += "0"; 
            }
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "1";
                estado = false;
            }
            else
            {
                txtVisor.Text += "1";
            }
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "2";
                estado = false;
            }
            else
            {
                txtVisor.Text += "2";
            }
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "3";
                estado = false;
            }
            else
            {
                txtVisor.Text += "3";
            }
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "4";
                estado = false;
            }
            else
            {
                txtVisor.Text += "4";
            }
        } 

        private void btn5_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "5";
                estado = false;
            }
            else
            {
                txtVisor.Text += "5";
            }
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "6";
                estado = false;
            }
            else
            {
                txtVisor.Text += "6";
            }
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "7";
                estado = false;
            }
            else
            {
                txtVisor.Text += "7";
            }
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "8";
                estado = false;
            }
            else
            {
                txtVisor.Text += "8";
            }
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            if (estado)
            {
                txtVisor.Text = "9";
                estado = false;
            }
            else
            {
                txtVisor.Text += "9";
            }
        }

        private void btnsum_Click(object sender, EventArgs e)
        {
            try{
            n1 = txtVisor.Text;
            c.Op = "+";
            estado = true;
            }
            catch 
            {
                return;
            }
        }

        private void btnmin_Click(object sender, EventArgs e)
        {
            try{
            n1 = txtVisor.Text;
            c.Op = "-";
            estado = true;
            }
            catch 
            {
                return;
            }
        }

        private void btnmult_Click(object sender, EventArgs e)
        {
            try{
            n1 = txtVisor.Text;
            c.Op = "*";
            estado = true;
            }
            catch 
            {
                return;
            }
        }

        private void btndiv_Click(object sender, EventArgs e)
        {
            try{
            n1 = txtVisor.Text;
            c.Op = "/";
            estado = true;
            }
            catch 
            {
                return;
            }
        }

        private void btnraiz_Click(object sender, EventArgs e)
        {
            try{
            n1 = txtVisor.Text;
            double doubleN1 = Convert.ToDouble(n1);

            double result = Math.Sqrt(doubleN1);
            txtVisor.Text = Convert.ToString(result);
            }
            catch 
            {
                return;
            }
        }

       private void btnporc_Click(object sender, EventArgs e)
        {
           try{
            double N1 = Convert.ToDouble(n1); 
            double N2 = Convert.ToDouble(txtVisor.Text);
            txtVisor.Text = Convert.ToString(N1 * N2 /100);
           }
           catch 
           {
               return;
           }
        }

        private void btnigu_Click(object sender, EventArgs e)
        {
            try{
            n2 = txtVisor.Text;
            txtVisor.Text = c.Igual(n1, n2);
            }
            catch 
            {
                return;
            }
        }

        private void btnmod_Click(object sender, EventArgs e)
        {
            try{
            n1 = txtVisor.Text;
            c.Op = "mod";
            estado = true;
            }
            catch 
            {
                return;
            }
        }

        private void btnlog_Click(object sender, EventArgs e)
        {
            try{
            n1 = txtVisor.Text;
            c.Op = "log";
            estado = true;
            }
            catch 
            {
                return;
            }
        }

        private void btnvirgula_Click(object sender, EventArgs e)
        {
            try{
            int virg;
            virg = txtVisor.Text.IndexOf(",");   
            if (virg >= 0)                    
                return;
            else                           
            txtVisor.Text += ",";
            }
            catch 
            {
                return;
            }
        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            txtVisor.Text = "";
            n1 = "0";
            n2 = "0";
            c.Op = "0";
        }

        private void btnback_Click(object sender, EventArgs e)
        {
            try{
            char[] arr = txtVisor.Text.ToCharArray();

            string txt = "";

            int tamanho = arr.Length;

            for (int i = 0; i < arr.Length; i++)
            {

                if (i + 1 != tamanho)
                {

                    txt += arr[i].ToString();

                }

            }

            txtVisor.Text = txt;
            }
            catch 
            {
                return;
            }

        }

        private void btnpot_Click(object sender, EventArgs e)
        {
            try{
            double N1 = Convert.ToDouble(txtVisor.Text);
            txtVisor.Text = Convert.ToString((N1 * N1) * N1);
            }
            catch 
            {
                return;
            }
        }

        private void btn_inverter_Click(object sender, EventArgs e)
        {
            try{
            double n1 = double.Parse(txtVisor.Text) * (-1);
            txtVisor.Text = n1.ToString();
            }
            catch 
            {
                return;
            }
        }

        private void btn_quadrado_Click(object sender, EventArgs e)
        {
            try{
            double N1 = Convert.ToDouble(txtVisor.Text);
            txtVisor.Text = Convert.ToString(N1 * N1);
            }
            catch 
            {
                return;
            }
        }

        private void btn_inv_Click(object sender, EventArgs e)
        {
            try
            {
                double d = double.Parse(txtVisor.Text);
                if (d == 0)
                {
                    return;
                }
                else
                {
                    d = 1 / d;
                    txtVisor.Text = d.ToString();
                }
            }
            catch
            {
                    return;
            }
        }

        private void btnCE_Click(object sender, EventArgs e)
        {
            try{
            txtVisor.Text = "";
            }
            catch 
            {
                return;
            }
        }

        private void btnmmais_Click(object sender, EventArgs e)
        {
            try{
            memory += Convert.ToDouble(txtVisor.Text);
            txtVisor.Clear();
            }
            catch 
            {
                return;
            }
        }

        private void btnmclear_Click(object sender, EventArgs e)
        {
            try{
            memory = Convert.ToDouble("0");
            txtVisor.Clear();
            }
            catch 
            {
                return;
            }
        }

        private void btnmmenos_Click(object sender, EventArgs e)
        {
            try{
            memory -= Convert.ToDouble(txtVisor.Text);
            txtVisor.Clear();

            }catch{
                return;
            }
        }

        private void btnmresult_Click(object sender, EventArgs e)
        {
            try{
            txtVisor.Text = Convert.ToString(memory);
            }
            catch
            {
                return;
            }
        }

        private void btnmsave_Click(object sender, EventArgs e)
        {
            try{
            memory = Convert.ToDouble(txtVisor.Text);
            txtVisor.Clear();
            }
            catch
            {
                return;
            }
        }

        private void txtVisor_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_KeyPress(object sender, KeyEventArgs e)
        { 
            if (e.KeyCode == Keys.Add) 
            {
                btnsum.PerformClick();
            }
            else if (e.KeyCode == Keys.Subtract)
            {
                btnmin.PerformClick();
            }
            else if (e.KeyCode == Keys.Multiply)
            {
                btnmult.PerformClick();
            }
            else if (e.KeyCode == Keys.Divide)
            {
                btndiv.PerformClick();
            }
            else if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Separator)
            {
                btnigu.PerformClick();
            }
            else if (e.KeyCode == Keys.D0 || e.KeyCode == Keys.NumPad0)
            {
                btn0.PerformClick();
            }
            else if (e.KeyCode == Keys.D1 || e.KeyCode == Keys.NumPad1)
            {
                btn1.PerformClick();

            }
            else if (e.KeyCode == Keys.D2 || e.KeyCode == Keys.NumPad2)
            {
                btn2.PerformClick();

            }
            else if (e.KeyCode == Keys.D3 || e.KeyCode == Keys.NumPad3)
            {
                btn3.PerformClick();

            }
            else if (e.KeyCode == Keys.D4 || e.KeyCode == Keys.NumPad4)
            {
                btn4.PerformClick();

            }
            else if (e.KeyCode == Keys.D5 || e.KeyCode == Keys.NumPad5)
            {
                btn5.PerformClick();

            }
            else if (e.KeyCode == Keys.D6 || e.KeyCode == Keys.NumPad6)
            {
                btn6.PerformClick();

            }
            else if (e.KeyCode == Keys.D7 || e.KeyCode == Keys.NumPad7)
            {
                btn7.PerformClick();
            }
            else if (e.KeyCode == Keys.D8 || e.KeyCode == Keys.NumPad8)
            {
                btn8.PerformClick();

            }
            else if (e.KeyCode == Keys.D9 || e.KeyCode == Keys.NumPad9)
            {
                btn9.PerformClick();

            }
            else if (e.KeyCode == Keys.Decimal || e.KeyCode == Keys.OemPeriod || e.KeyCode == Keys.Oemcomma || e.KeyCode == Keys.None)
            {
                btnvirgula.PerformClick();

            }
            else if (e.KeyCode == Keys.Back)
            {
                btnback.PerformClick();

            }
            else if (e.KeyCode == Keys.Delete)
            {
                btnclear.PerformClick();

            }
            else if (e.KeyCode == Keys.Delete)
            {
                btnback.PerformClick();

            }
            else if (e.Modifiers == Keys.Shift && e.KeyCode == Keys.D5)
            {
                btnporc.PerformClick();
            }


        }

    }

}
    
asked by anonymous 24.04.2017 / 19:42

1 answer

3

You will have to combine the KeyCode and To form the "%": %% (%%) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if (e.Modifiers == Keys.Shift && e.KeyCode == Keys.D5)
    //seu código

or directly access the #

if (e.Shift && e.KeyCode == Keys.D5)
    //seu código

Response update

To perform the percentage operation without this key being inserted into the textbox, you will have to use the KeyPress event. The KeyDown event, which you used, is after the key has already been pressed, so you would have to do a POG to get around this - unnecessary.

See how it should look like:

private void frmCalculadora_KeyPress(object sender, KeyPressEventArgs e) {
    if (e.KeyChar.ToString() == "%") {
        btnPercent.PerformClick();
        e.Handled = true;
    }
}

To prevent the character from being entered into the text, let's set the property to Modifiers Shift after calling the click event.

    
24.04.2017 / 20:05