none
Converting Key To ASCII

    Question

  • Greetings,
    how can I convert Key to ASCII. To point is that I would like to have to same result as using ToAsciiEX in Winapi (user32.dll).
    For instance: I would like to check whether Key.LeftShift has an equivalent in ASCII table - as we can see from the link: http://www.bbdsoft.com/ascii.html[^] there is no equivalent in the ASCII table so the ToAsciiEx function will return 0.
    For instance: CTRL+B has it's equivalent in ASCII table equals 2
    I would like to know whether SHIFT+A has equivalent in ASCII table.
    Generally i would like to check whether pressed keys (for instance: simple A, 0,4 or CTRL+B) have equivalent in ASCII table
    • Edited by niao Friday, July 03, 2009 8:46 AM
    Friday, July 03, 2009 6:16 AM

Answers

All replies

  • Why don't you call ToAsciiEx if it does what you want?

    Mattias, C# MVP
    Friday, July 03, 2009 9:26 AM
  • I don't want to call WinAPI functions from c# .net.
    Friday, July 03, 2009 9:30 AM
  • Why not?
    Mattias, C# MVP
    Friday, July 03, 2009 10:00 AM
  • Getting into the business of translating virtual keys yourself is something you want to avoid.  ToAsciiEx() is pretty hard to use, requiring the scan code and the keyboard state.  And the active keyboard layout is crucial.  Windows already does this for you, typing keys produce the WM_CHAR message.  In .NET, the KeyPressed event.

    Hans Passant.
    Friday, July 03, 2009 10:36 AM
    Moderator
  • hmm. The case is that I want to read string scanned by the barcode scanner i am doing this in such a way:
    private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
            {
                try
                {
                    short s2 = Convert.ToInt16(e.Key);
                    System.Diagnostics.Debug.Print(s2.ToString());
    
                    if (Key.LeftCtrl == e.Key && false == _IsReadingIdentifyingNumber)
                    {
                        _IsReadingIdentifyingNumber = true;
                    }
                    else if (_IsReadingIdentifyingNumber && true == _IsReadingIdentifyingNumber)
                    {
                        if (Key.LeftCtrl == e.Key)
                        {
                            _IsReadingIdentifyingNumber = false;
                        }
                    }
                    if (_IsReadingIdentifyingNumber)
                    {
                        e.Handled = true;
                    }
                    _IdentifyingNumberReader.ReadSingleKey(e.Key);
                }
                catch (BarcodeReaderException ex)
                {
                    CLogger.LogException(ex);
                }
                catch (Exception ex)
                {
                    CLogger.LogException(ex);
                }
            }
    The point is that I receive something like LeftCtrl and then B (that should mean "startText" - ascii code equals 2) , LEFTSHITF+A (means that typeof barcode is Code39).
    How can I handle this?
    Friday, July 03, 2009 11:17 AM
  • Bar code readers usually can operate in two modes.  One is where it acts as a keyboard wedge, generating keystrokes as though the user typed the code.  Another is where an application program receives the scanned code directly, usually through a serial port.  You've got the reader switched into a mode where it generates codes suitable for the latter way.  Ctrl+B is a control code, SOH (Start Of Header).   It is used to synchronize the program with the reader, SOH indicates the start of a scan, you'd use it to reset a buffer index.

    You'll want to configure the reader for the keyboard wedge mode where it won't generate the control characters.  You can still make it work if necessary by writing both a KeyDown event handler, to detect the SOH, and a KeyPressed event handler, to detect the scan codes.  Avoid trying to convert key strokes you see in KeyDown to characters, KeyPressed already does this for you.

    Hans Passant.
    Friday, July 03, 2009 12:33 PM
    Moderator
  • Thanks nobugz, I understand. But can you please let me know how can i determine what kind of barcode is scanned. I have a three different scanners and I receive the string like that:
    1) Ctrl B Shift A
    2) Ctrl B Shift Shift A
    3) Ctrl B A

    Where A tells us that Code39 barcode has been scanned.
    Should I then remove all Shift control from that string? Is a Shift an important thing here in that scanned code?

    I would like to add that I am using WPF and I cannot find KeyPressed handler.
    Monday, July 06, 2009 5:57 AM
  • You still seem to try to read with KeyDown instead of KeyPress.  I've never seen a scanner that indicates the barcode type, it's the scanner's job to hide that.  Make sure you are scanning a real barcode instead of one from a sheet that's used to configure the scanner.

    Hans Passant.
    Monday, July 06, 2009 6:18 AM
    Moderator
  • nobugz,
    I am using the real barcode.
    The point is that WPF has no KeyPress event? What kind of event should I use instead?
    Using KeyDown Event i received the following Keys:
    LeftCtrl
    B
    LeftShift
    A
    D1
    D0
    D4
    D0
    D1
    D6
    D3
    D2
    D8
    D8
    D6
    D9
    LeftCtrl
    C

    Monday, July 06, 2009 6:35 AM
  • I'd guess at TextInput.  Ask at the .NET WPF forum.

    Hans Passant.
    Monday, July 06, 2009 6:42 AM
    Moderator
  • [DllImport( "user32.dll" )]
    static extern int ToAsciiEx( uint uVirtKey, uint uScanCode, byte[] lpKeyState, [Out] StringBuilder lpChar, uint uFlags, IntPtr hkl );
    
    
    ..
    
    private void lvw_OnPreviewKeyUp( object sender, KeyEventArgs e ) {
    	Debug.WriteLine( ToString( e.Key ) );
    }
    
    ...
    
    public static string ToString( Key key ) {
    
    	var keyboardState = new byte[256];
    
    	GetKeyboardState( keyboardState );
    
    	var sb = new StringBuilder( );
    
    	var keyCode = KeyInterop.VirtualKeyFromKey( key );
    
    	var i = ToAsciiEx( (uint) keyCode, 0, keyboardState, sb, 0, IntPtr.Zero );
    
    	if ( i == 1 )  return sb.ToString( );
    
    	// a dead key was clicked .. ignore
    	return "";
    
    }
    
    
    I hope this snipet was helpfull
    Friday, April 30, 2010 4:10 PM