我知道條形碼掃描器就像一個鍵盤一樣。我需要的是具有某種api(.net/c#優選)的掃描儀,我可以插入到我的應用程序中。有沒有一個特定的掃描儀硬件供應商 devs已經使用或正在使用,有一個.net API?或者我可以使用任何掃描器並在其周圍構建api或使用其中一種開源軟件?有沒有可以用於集成到我的應用程序的.net api的條形碼掃描儀(硬件)?
所以我想做的一件事就是能夠將光標放在特定的文本字段上而不需要USB接口掃描儀的數值。
感謝
我知道條形碼掃描器就像一個鍵盤一樣。我需要的是具有某種api(.net/c#優選)的掃描儀,我可以插入到我的應用程序中。有沒有一個特定的掃描儀硬件供應商 devs已經使用或正在使用,有一個.net API?或者我可以使用任何掃描器並在其周圍構建api或使用其中一種開源軟件?有沒有可以用於集成到我的應用程序的.net api的條形碼掃描儀(硬件)?
所以我想做的一件事就是能夠將光標放在特定的文本字段上而不需要USB接口掃描儀的數值。
感謝
要留在這就像一個鍵盤,你也可以嘗試掃描,來檢測你的整個應用程序內按下所有關鍵。要在您的應用程序中獲得KeyPress
事件,您應將Form.KeyPreview
設置爲true
並註冊到上述表單的事件。
現在,在發送到當前活動控件之前,您會收到每個密鑰,並且您可以使用它,無論您喜歡什麼。爲防止在完成工作後它將發送到當前的活動控件,請將e.Handled
設置爲true
。
但是這種解決方案具有絕對兩個缺點:
我已經實現了使用條形碼掃描儀類似的系統。我是很確定(不是100%很久以前),文本作爲一個大字符串進來,只提出了一個按鍵事件。因此,您可以做的是將輸入字符串與典型的條形碼字符串進行匹配,例如KeyPressEvent,如果匹配,則查找該項目。或者甚至只是檢查字符串中字符的長度(就好像它的鍵盤輸入一次只能是1個字符一樣)。
您是否在基本表單上測試過它?
嗯,這取決於。如果你有一個USB條形碼掃描器,它就像一個鍵盤一樣,直接讀取掃描文本到一個集中控制。如果您有串行條碼掃描器,則需要使用.NET中的SerialPort
類。
如果您在填寫文本框之前需要解析數據等,那麼最好的辦法就是使用串行掃描器。
下面是使用code sample:
public partial class Form1 : Form
{
SerialPort _serialPort;
// delegate is used to write to a UI control from a non-UI thread
private delegate void SetTextDeleg(string text);
private void Form1_Load(object sender, EventArgs e)
{
// all of the options for a serial device
// can be sent through the constructor of the SerialPort class
// PortName = "COM1", Baud Rate = 19200, Parity = None,
// Data Bits = 8, Stop Bits = One, Handshake = None
_serialPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
_serialPort.Handshake = Handshake.None;
_serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
}
private void btnStart_Click(object sender, EventArgs e)
{
// Makes sure serial port is open before trying to write
try
{
if(!_serialPort.IsOpen)
_serialPort.Open();
_serialPort.Write("SI\r\n");
}
catch (Exception ex)
{
MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
}
}
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(500);
string data = _serialPort.ReadLine();
this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
}
private void si_DataReceived(string data)
{
textBox1.Text = data.Trim();
}
}
感謝Oli,這聽起來更像我正在尋找的解決方案。我會認爲掃描儀通常需要開發人員構建一個API來處理他們想要掃描儀的行爲? – simplyme