Solved

Keyboard buffer

Posted on 1998-12-06
8
2,318 Views
Last Modified: 2008-02-20
I would like to clear the keyboard buffer in VB. Is there an API that will examine the buffer contents and allow removal or manipulation of the buffer contents.
0
Comment
Question by:ashmore
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 1448425
Hi ashmore,

You can use SetKeyboardState and GetKeyboardState. Came across a good example at the microsoft site with a form and a button and this was the code

Private Declare Sub GetKeyboardState Lib "user32" (lpKeyState As Any)
Private Declare Sub SetKeyboardState Lib "user32" (lpKeyState As Any)
Const VK_CAPITAL = &H14
Const VK_NUMLOCK = &H90

Private Sub Command1_Click()
    ReDim KeyboardBuffer(256) As Byte

    GetKeyboardState KeyboardBuffer(0)

    If KeyboardBuffer(VK_CAPITAL) And 1 Then
        KeyboardBuffer(VK_CAPITAL) = 0
    Else
        KeyboardBuffer(VK_CAPITAL) = 1
    End If

    If KeyboardBuffer(VK_NUMLOCK) And 1 Then
        KeyboardBuffer(VK_NUMLOCK) = 0
    Else
        KeyboardBuffer(VK_NUMLOCK) = 1
    End If

    SetKeyboardState KeyboardBuffer(0)
End Sub

Bruintje.

Aah... found the link so you can check it for yourself

http://premium.microsoft.com/msdn/library/techart/msdn_msdn167.htm

0
 
LVL 15

Expert Comment

by:ameba
ID: 1448426
If by 'buffer' you mean 'system queue' you can use:

GetInputState
- the return value is nonzero if the function detects a mouse click or keyboard event in the system queue. Otherwise, it is zero.

GetQueueStatus (fuFlags)
- returns a value that indicates the type of messages in the queue.
QS_KEY      WM_CHAR message is in the queue.

Both functions are very fast and typically used inside speed-critical loops.
---------
There is API SetMessageQueue, but I wouldn't try it in VB
(The function must be called before any windows are created and before any messages are sent)

0
 

Author Comment

by:ashmore
ID: 1448427
Dear Bruintje
Thanks for your reply but I need to explain more.
The GetKeyboardstate lets you know the state of any of the 256 virtual keys. The problem I am trying to solve is that if for  example the <esc> or <enter> key has been pressed 6 times it only registers in  GetKeyboardstate as being pressed once, not how many times. What I need to do is to look at the the most recent unprocessed keystrokes, analyse what the user is doing and then set the buffer to null. It must be a feature that is used in writing games. It was easy in DOS but maybe thats showing my age.
0
 
LVL 15

Expert Comment

by:ameba
ID: 1448428
ameba wrote:
>by 'buffer' you mean 'system queue'
You do!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 44

Expert Comment

by:bruintje
ID: 1448429
Hi Ashmore,

Well reject my answer and give ameba a chance. Thinks his comments are more of an solution to your problem.
0
 
LVL 15

Expert Comment

by:ameba
ID: 1448430
I don't have solution. I suggest ashmore use
  If GetQueueStatus(QS_KEY)<>0 Then ' something new is in the queue
    If GetKeyState(VK_ENTER) < 0 Then
        ' set some public flags that indicate enter pressed
    ElseIf GetKeyState(VK_ESCAPE) < 0 Then
        ' set some other public flags
    End If
  End If

and use some logic that examines flags in key processing routine.

0
 

Author Comment

by:ashmore
ID: 1448431
Rejected on  bruintje advice. Thank you for your help.
0
 

Accepted Solution

by:
kishor33 earned 200 total points
ID: 1448432
You are not required to use a API call for clearing the content of the keyboard instead use the combination of doevent procedure and keydown event to clear the content of the keyboard buffer.

Hope it will help

Thank you,
Birendra
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now