coffeemug22
coffeemug22

Reputation: 9

how to make sure that function only fires once (RawInputEventArg)

I'm currently working on a method that gives the user the possibility to add a handscanner to a dicitionary in order to scan some barcodes with it. (before i started the scanners were hardcoded in the dictionary). my colleague from which i got this project, implemented the rawinput_dll in order to get all of the necessary data from the barcode scanner. The method to get the data is shown below:

private void OnKeyPressed(object sender, RawInputEventArg e)
        
        {
            
            if (!Scanners.ContainsKey(e.KeyPressEvent.DeviceName)) 
            {
                
                return;
            }
            else if (Scanners.ContainsKey(e.KeyPressEvent.DeviceName))
            {
                if (e.KeyPressEvent.KeyPressState == "MAKE")
                {       
                    return;
                } 

                if (e.KeyPressEvent.VKeyName != "\n")
               
                {
                    scanNumber += e.KeyPressEvent.VKeyName;
                    return;
                }
                    devID = e.KeyPressEvent.DeviceName;
                    Debug.Print(devID);
                        Aufrufen(scanNumber);
                    scanNumber = "";
                    
            }
        }

Basically there are three classes in this program (FrmMenu, FrmSettings and a Class for the Scanner itself). If you want to add settings for the program you click on a button that opens up a new instance of FrmSettings

  private void BtnSettings_Click(object sender, EventArgs e)
        {
            FrmSettings settings = new FrmSettings();
            settings.ShowDialog();
            settings.BtnSave_Click(sender, e);
            settings.Dispose();
            
        }

In this form there 2 buttons where you can choose if you want to add a scanner that scans even numbers or one that scans odd ones. If you press one of the buttons you need to scan a barcode in order to get the information (VID of Scanner) which is used as key to add the new scanner to the dictionary.

private void OnKeyPressed(object sender, RawInputEventArg e)
        
        {
            if (newScanner == true)
            {
                devIDnew = e.KeyPressEvent.DeviceName;
                scannerAnlegen(devIDnew);
            }
            
        }

scannerAnlegen is the methode that adds the scanner to the dict.

public void scannerAnlegen(string devIDnew)
        {
            if(EvenOrOdd == true)
            {
                Scanner ger = new Scanner("dev3", "even");
                FrmMenu.Scanners.Add(devIDnew, ger);
                newScanner = false;
            }
            else
            {
                Scanner ug = new Scanner("dev4", "odd");
                FrmMenu.Scanners.Add(devIDnew, ug);
                newScanner = false;
            }

        }

my problem rn is, that it seems like i cant get out of this OneKeyPressed method of the Settings class. the logic of the OneKeyPressed method of the FrmMenu Class is that it can only proceed if the scanner is in the dictionary. Adding the scanner seems to work because when i debug and try to add one scanner the second time it throws and exception and says something like "element with this key already added". But why does this code doesn't continue then?

Upvotes: 0

Views: 87

Answers (0)

Related Questions