|
The purpose of it is that i want the program will read data from a barcode reader and then display it on the textbox, even if the window is not it focus
|
|
|
|
|
If the barcode reader emulates a keyboard, then you can't - at least not without hooking into the OS and diverting every keystroke to your app, which may annoy the user, and probably not work in anything with a virus scanner running!
How does your BC reader work? Does it have a serial or USB interface that does not emulate a keyboard?
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
Yes,
It works as keyboard amulator...
|
|
|
|
|
Then you have a problem. all the Keyboard emulating BCRs I have seen read teh BC and just provide a datastream as if the barcode data had been typed by the user.
For example, if the BC was 092939201610 then the BCR fakes teh keyboard keystrokes for '0' down, '0 up, '9' down, '9' up, etc. There is no way to tell if the real KB or the BCR provided the data, so would have to feed all keyboard data to your progam. As a user this would: 1) be very annoying and 2) look like a memory resident key logger.
Your best bet is to talk to the manufacturer and see if they have done similar before.
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
OriginalGriff wrote: Then you have a problem. all the Keyboard emulating BCRs I have seen read teh BC and just provide a datastream as if the barcode data had been typed by the user.
I feel it's all ok. User such as a cashier isn't supposed to use a keyboard very often. So we can redirect BC stream to the textbox. In the textbox handler we can validate BC/user input. For example - user always start typing session with "A" char, else it's BC's chars.
|
|
|
|
|
I can understand if you are developing a POS system you may want this input
to be directed to the textbox.
You could always try setting the KeyPreview to True on the Form.
Then on the KeyPress you could try
private void YourForm_KeyPress(object sender, KeyPressEventArgs e)
{
ScannerFirstKeyValue = e.KeyChar.ToString();
if (this.ActiveControl != this.YourTextBox)
{
YourTextBox.AppendText(ScannerFirstKeyValue);
}
this.ActiveControl = YourTextBox;
}
This would ensure all input is placed in correct textbox.
Remember if you have multiple textboxes this will be a problem!!
Unless the BC Reader has some sort of prefix that can distinguish it from a keyboard.
Regards
Mick Curley
|
|
|
|
|
doesn't that prevent people from typing anything in any other Control?
|
|
|
|
|
But we still can distinguish BC from user by using prefixes. What do you think?
|
|
|
|
|
Hi,
I need this thing only to read data from barcode reader.
Is there a way so only data from it (COM1 port for example) will be insereted to that textbox or the clipboard even if the application is not in focus (in background)?
|
|
|
|
|
Although it's rather unwieldy task I guess I know a way to perform it.
To interact with an application that is not in focus I recommend you to use windows application log.
Create the window service to handle BC input and write it down to the Log. In the application - check the Log for new entries on timer event and if it exists put it into your textbox. So despite your application might be not in focuse it still can achieve data from BC.
|
|
|
|
|
i think a better way is to create a windows service that will read the data from the Barcode reader and then put in in the clipboard.
But i don't know how to create such service and how to read from this device (USB port)
|
|
|
|
|
So, we have a result here. To accomplish the mission you should definitly create a service. With hooks!
|
|
|
|
|
I think that you need create a windows service. When the computer got data, the service app show the data on the form's textbox, even the form unfocused.
|
|
|
|
|
OK how can i do it using my application?
|
|
|
|
|
Oh!
I try to open a win app from a windows service, but I was failed.
I consider that creating a socket connect between the windows service and the win app may be practicable.
creating windows service like this.
creating socket like this.
|
|
|
|
|
OK. But i have another question-
If i create a windows service to manage the data from the barcode reader (the data will go to clipboard and then set focus to my application),
How can i install this windows service on a user computer (without installutil etc.)?
|
|
|
|
|
You could install the windows service like this, if u are using .Net FrameWork 3.5.
I found that the file named "System.Configuration.Install.dll" in .Net 2.0 is different from the .Net 3.5's.
There are some simple code.
using System.ServiceProcess;
using System.Configuration.Install;
using System.Collections;
private bool isServiceIsExisted(string NameService)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName.ToLower() == NameService.ToLower())
{
return true;
}
}
return false;
}
private void InstallmyService(IDictionary stateSaver, string filepath)
{
AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
AssemblyInstaller1.UseNewContext = true;
AssemblyInstaller1.Path = filepath;
AssemblyInstaller1.Install(stateSaver);
AssemblyInstaller1.Commit(stateSaver);
AssemblyInstaller1.Dispose();
}
private void UnInstallmyService(string filepath)
{
AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
AssemblyInstaller1.UseNewContext = true;
AssemblyInstaller1.Path = filepath;
AssemblyInstaller1.Uninstall(null);
AssemblyInstaller1.Dispose();
}
Good Luck!
|
|
|
|
|
Will this code start the service too?
|
|
|
|
|
If set the windows service autorun, when you restart computer, the service will start.
Of course, you can start the service in manual like this.
private bool StarmyService(string name)
{
ServiceController sc = new ServiceController(name);
if (sc.Status == ServiceControllerStatus.Stopped || sc.Status == ServiceControllerStatus.StopPending
)
{
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10));
}
else
{
}
sc.Close();
return true;
}
And stop the service like this
private bool StopmyService(string name)
{
ServiceController sc = new ServiceController(name);
if (sc.Status == ServiceControllerStatus.Running ||
sc.Status == ServiceControllerStatus.StartPending)
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 10));
}
else
{
}
sc.Close();
return true;
}
|
|
|
|
|
OK and how can i set the service to start automatically ? i mean in the service code.
Also how can i check if the service is already running?
|
|
|
|
|
OK, the simple code to check if the service is running like this.
private bool IsRunning(string name)
{
bool IsRun = false;
if (!isServiceIsExisted(name))
{
return false;
}
ServiceController sc = new ServiceController(name);
if (sc.Status == ServiceControllerStatus.StartPending ||
sc.Status == ServiceControllerStatus.Running)
{
IsRun = true;
}
sc.Close();
return IsRun;
}
Set the service to start automatically:
Change the value of property named "StartType" of the windows service to "Automatic".
BTW, owing to the limitation of my knowledge, the code are not very strong.I suggest that you can research them by yourself. I think that after you reserach them, you will learn more.
|
|
|
|
|
OK , and how can i set the service to start automaticallu ? i mean programatically?
also how do i check if the service is already running? or it's not matter?
|
|
|
|
|
The function "IsRunning" will check if the windows service is running.
I think that when creating a windows service just like creating a application, so I do not know how to create it programatically. We must set the windows service before we release it.
|
|
|
|
|
I feel that using a socet connection between service and application is not a good idea because this connection can be terminated by firewall etc
ps use logging instead
|
|
|
|
|
I.C.
But, just remind once.
I know little about the Logging, I will have some research about that.
|
|
|
|