none
How to capture any keystroke RRS feed

  • Question

  • I am writing a text-based adventure game using VBA for Excel. Valid responses from the user are always one-character values. I currently have the user interface set up to use an InputBox to display prompt information and accept input. This requires an unneeded keystroke to click "OK" or press "Enter", since only the first character entered will be processed. I would like to change this to display prompt info in a MsgBox, and then check for any key pressed (not within any kind of box or control) and immediately process the input. I think I would want to use the KeyPress or KeyDown event, but all of the examples I find are for capturing the keystroke within a TextBox, which is waiting for input. I would need have the code looping with a pause, and detect and process any key being pressed. I wouldn't need to distinguish between the actual character value, based on using Shift key (for caps vs. lowercase, or numerals vs. special characters), so just capturing the actual key pressed would be fine. Thanks for any suggestions!
    Monday, March 16, 2020 6:55 AM

All replies

  • To:  BillRow
    re:  capture keystroke

    Put up a userform with a Textbox and an Exit button...
    [Edited - line added below]
    Changes show below allow use of worksheet while UserForm displayed.



    Code in a standard module [Edited]...
    Sub GetGoing()
    UserForm1.Show False
       'Unload UserForm1   '<<< not done here, see below
    End Sub

    '---
    Code in the UserForm [Edited]...
    Private Sub CommandButton1_Click()
    Me.Hide
    Unload Me
    End Sub

    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
      On Error GoTo BadKey
      Dim strChar As String
      strChar = ChrW$(KeyAscii)
     
      'do something with the character entered (strChar)
     
      TextBox1.Value = vbNullString
      Exit Sub
    BadKey:
      Beep
      Resume Next
    End Sub
    '---

    The free Excel workbook "Professional_Compare" compares every cell Or
    every row in two worksheets with a choice of compare type.
    Includes "Clean Data" and "Quick Uniques List" utilities.
    Download (no ads) from:  http://www.mediafire.com/folder/lto3hbhyq0hcf/Documents


    Monday, March 16, 2020 11:40 PM
  • Thanks for the response. I tried pasting this code in and running, but get an error. Error is "User-defined type not defined" on the line "Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)". I'm probably not doing this as I should. See code below. More advice?? Also, to be clear, my goal is to not require any kind of text box for input, but just to capture any keystroke after MsgBox is used to display info. Thanks!

    -------

    Sub GetGoing()
    UserForm1.Show False
       'Unload UserForm1   '<<< not done here, see below
    End Sub

    Private Sub CommandButton1_Click()
    Me.Hide
    Unload Me
    End Sub

    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
      On Error GoTo BadKey
      Dim strChar As String
      strChar = ChrW$(KeyAscii)
     
      'do something with the character entered (strChar)
     
      TextBox1.Value = vbNullString
      Exit Sub
    BadKey:
      Beep
      Resume Next
    End Sub

    Wednesday, March 18, 2020 4:02 AM
  • To:  BillRow
    re:  code misfires

    The GetGoing code goes in a standard module.
    The Private Sub CommandButton1_Click() and the
          Private Sub TextBox1_KeyPress code goes in the UserForm module.
          (right-click the UserForm and choose "View Code")

    There is no vba code to catch key strokes from a worksheet.
    Something might be able to be created using the Windows API (or might not).
    I'm not the one to do that (if possible) and the ones who might seem to have retired or now avoid the forums.

    '---

    http://www.mediafire.com/folder/lto3hbhyq0hcf/Documents

    Wednesday, March 18, 2020 5:11 AM
  • Thanks again. I'll probably just leave it as is, using an InputBox. Not really a big deal to have to hit Enter after each response. I was trying to make it as simple as possible. Sounds like any possible solution would be well beyond my skill set. Thanks!
    Wednesday, March 18, 2020 5:54 AM