I am able to make use of this third party product, http://globalmousekeyhook.codeplex.com/
which is able to extract keystroke which is outside C# application, e.g like in Notepad.
However, I ONLY want to extract the ENTER keystroke. that is:
Say for instance, if user press "123" followed by the ENTER, the count would trigger 1, not 4.
And the next instance if user press 456, followed by the ENTER, the count would trigger 2.
Is it possible?
I try to put under one of those Key event, like HookManager_KeyUp, it counts also non-ENTER keys, which I do not want. So where should I put my code under?
VulpesPosted Feb 1, 2013, 5:17 AM
foreach (string x in serialNumberArray)
{
if (!x.Contains(new string(serialNumber)))
{
Enter_count++;
sevenSegmentArrayActual.Value = Enter_count.ToString();
sevenSegmentArrayDifference.Value = (Convert.ToInt32(sevenSegmentArrayTarget.Value) - Convert.ToInt32(sevenSegmentArrayActual.Value)).ToString();
SaveSerialNumber(new string(serialNumber));
digit_count = 0;
Array.Clear(serialNumber, 0, 14);
}
}
with this:
string sn = new string(serialNumber);
if (!serialNumberArray.Contains(sn))
{
Enter_count++;
sevenSegmentArrayActual.Value = Enter_count.ToString();
sevenSegmentArrayDifference.Value = (Convert.ToInt32(sevenSegmentArrayTarget.Value) - Convert.ToInt32(sevenSegmentArrayActual.Value)).ToString();
SaveSerialNumber(sn);
}
digit_count = 0; // need to reset whether s/n duplicated or not
Array.Clear(serialNumber, 0, 14);
TAN WhoAMIPosted Jun 6, 2013, 8:31 PM
VulpesPosted Jun 6, 2013, 11:22 AM
TAN WhoAMIPosted Jun 5, 2013, 11:44 PM
I attached the code, and it seems to have some bugs which is not easy to trace. Most of the time, when a less than 14-digits is entered, it is not captured, which is correct.
But there are some few rare instances when a less than 14-digits is entered, it is captured, as seen in the output file as attached.
Those less than 14-digits serial numbers should not be captured in the output file:
06-Jun-13 10:31:11 AM 40000022492
06-Jun-13 10:32:01 AM 400000224922
06-Jun-13 10:32:21 AM 4000002249224
06-Jun-13 10:33:50 AM 4000002249225
6/6/2013 11:00:56 AM 99999999999
thank you in advance
TAN WhoAMIPosted Feb 1, 2013, 6:58 AM
TAN WhoAMIPosted Feb 1, 2013, 3:13 AM
Next, I want to make sure that no repeat of the 14-digit SerialNumber is stored in the output.txt file. Also, if the same serial number is entered again, the counter does not increase.
I try with the attached code, but it does not work. Would need your help again :)
TAN WhoAMIPosted Jan 27, 2013, 8:53 PM
I appreciate your kind help a lot. :)
VulpesPosted Jan 25, 2013, 6:28 AM
private void SaveSerialNumber(string SerialNumber)
{
TextWriter tw = new StreamWriter("Output.txt", true);
tw.WriteLine(string.Concat(DateTime.Now, "", SerialNumber));
tw.Close();
}
The previous code then needs changing to:
private int count = 0;
private int digits = 0;
private char[] serialNumber = new char[14]; // field to store digits entered
private void HookManager_KeyUp(object sender, KeyEventArgs e)
{
Log(string.Format("KeyUp \t\t {0}\n", e.KeyCode));
if ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) ||
(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9))
{
serialNumber[digits] = DigitFromKeyCode(e.KeyCode);
digits++;
if (digits > 14)
{
digits = 0;
Array.Clear(serialNumber, 0, 14);
}
}
else if (e.KeyCode == Keys.Enter && digits == 14)
{
count++;
label1.Text = count.ToString();
SaveSerialNumber(new string(serialNumber));
digits = 0;
Array.Clear(serialNumber, 0, 14);
}
else if (e.KeyCode == Keys.Back)
{
if (digits > 0)
{
digits--;
serialNumber[digits] = '\0';
}
}
else
{
digits = 0;
Array.Clear(serialNumber, 0, 14);
}
}
// add this method to extract digit from KeyCode
private char DigitFromKeyCode(Keys k)
{
string temp = k.ToString();
if (temp[0] == 'D' && temp.Length == 2)
{
return temp[1];
}
else if (temp.StartsWith("NumPad") && temp.Length == 7)
{
return temp[6];
}
else
{
return '\0';
}
}
TAN WhoAMIPosted Jan 25, 2013, 2:54 AM
that is, I want to call this function
private void SaveSerialNumber(string SerialNumber)
{
TextWriter tw = new StreamWriter("Output.txt");
tw.WriteLine(string.Concat(DateTime.Now, "", SerialNumber));
tw.Close();
}
VulpesPosted Jan 24, 2013, 5:59 AM
You need to add another field to count the digits entered and, if that has reached 14 when the ENTER key is pressed, increment the main count otherwise reset the digit count to zero and start again.
The problem is what to do when other keys are pressed. You could take a strict view on this and reset the digit count to zero if any other key is pressed at all!
This is fair enough for 'printing' characters such as letters and punctuation but what about non-printing characters such as the arrow keys etc?
It seems reasonable to at least allow the BACKSPACE key to be pressed to enable the user to correct what's already been entered and the following code therefore does that (decrementing the digit count if it's more than zero) but resets to zero if any other key is pressed at all:
TAN WhoAMIPosted Jan 23, 2013, 10:02 PM
TAN WhoAMIPosted Jan 23, 2013, 6:31 PM
VulpesPosted Jan 23, 2013, 5:57 AM
Try it like this:
private int count = 0; // best to start at 0 in case ENTER never pressed
Count_textBox.Text = count.ToString();