Results 1 to 7 of 7

Thread: hex + decimal keycodes utility

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Thumbs up hex + decimal keycodes utility

    heres a hex + decimal keycodes utility (see attachment)

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    4.         Label3.Text = "key - " & e.KeyCode.ToString
    5.         txtDecimal.Text = e.KeyCode
    6.         txtHexadecimal.Text = "&H" & Hex(Long.Parse(e.KeyCode))
    7.     End Sub
    8.  
    9. End Class
    Attached Files Attached Files

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: hex + decimal keycodes utility

    e.KeyCode is already a number. Implicitly converting it to a String in order to parse it in order to convert it to a String is a bit roundabout, not to mention that it won't compile with Option Strict On.
    vb.net Code:
    1. Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    2.     Label3.Text = "key - " & e.KeyCode.ToString()
    3.     txtDecimal.Text = CStr(e.KeyCode)
    4.     txtHexadecimal.Text = "&H" & e.KeyCode.ToString("X")
    5. End Sub

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: hex + decimal keycodes utility

    vb Code:
    1. e.KeyCode.ToString

    returns keyname - i.e. ctrl, alt tab etc.

    i didn't really need to put the
    vb Code:
    1. Long.Parse
    there though

  4. #4

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: hex + decimal keycodes utility

    heres the improved version

    vb Code:
    1. Public Class Form1    
    2.  
    3.     Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp        
    4.  
    5.         Label3.Text = "key - " & e.KeyCode.ToString        
    6.         txtDecimal.Text = e.KeyCode        
    7.         txtHexadecimal.Text = "&H" & Hex(e.KeyCode)
    8.  
    9.     End Sub
    10.  
    11. End Class

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: hex + decimal keycodes utility

    That still won't compile with Option Strict On, which everyone should have all the time unless they specifically need it Off. That's why I used this:
    vb.net Code:
    1. txtDecimal.Text = CStr(e.KeyCode)
    instead of this:
    vb.net Code:
    1. txtDecimal.Text = e.KeyCode
    Also, if you call the ToString with no parameters will return a string containing the enumerated value's label. If you do specify a parameter then it will not. When you call the ToString method of an integer value, which enums are, and specify "X" as the format string you'll get a hexadecimal string in upper case. That would be preferable to using the Hex Runtime function for many people, although ToString does pad to a 32-bit number with zeroes.

  6. #6

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: hex + decimal keycodes utility

    ok thanks. i didn't think of that

  7. #7
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: hex + decimal keycodes utility

    This will be usefull for APIs.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width