0% found this document useful (0 votes)
266 views2 pages

Key Detection with GetAsyncKeyState

This document contains code for a program that detects keyboard input in Visual Basic. It uses the GetAsyncKeyState function to check the state of keys on a timer interval. If certain keys are pressed, including numbers, operators, decimal point, and enter, it will display a message box indicating which key was pressed. The code checks the numeric pad keys separately from the main number keys.

Uploaded by

euverve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
266 views2 pages

Key Detection with GetAsyncKeyState

This document contains code for a program that detects keyboard input in Visual Basic. It uses the GetAsyncKeyState function to check the state of keys on a timer interval. If certain keys are pressed, including numbers, operators, decimal point, and enter, it will display a message box indicating which key was pressed. The code checks the numeric pad keys separately from the main number keys.

Uploaded by

euverve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C:\DocumentsandSettings\Stn02\mydocuments\visual...\Projects\GetAsyncKeyState\GetAsyncKeyState\Form1.

vb 1
ImportsSystem

.
Runtime
.
InteropServices
PublicClassForm

<
DllImport
("
user
32
.
dll
")
>_

PrivateSharedFunctionGetAsyncKeyState
(
ByValvKeyAsKeys
)
AsShort

EndFunction

PrivateWithEventsKeyboardKeyDetectTimerAsNewTimer

PrivateSubForm
1
_
Load
(
ByValsenderAsObject
,
ByValeAsEventArgs
)
HandlesMyBase
.
Load

KeyboardKeyDetectTimer
.
Interval
=
75

KeyboardKeyDetectTimer
.
Enabled
=
True

KeyboardKeyDetectTimer
.
Start
()

EndSub

PrivateSubKeyboardKeyDetectTimer
_
Tick
(
ByValsenderAsObject
,_

ByValeAsEventArgs
)
_

HandlesKeyboardKeyDetectTimer
.
Tick

'
NumericKeypad

DimNumPad
0
AsString
=
GetAsyncKeyState
(
Keys
.
NumPad
0
)

DimNumPad
1
AsString
=
GetAsyncKeyState
(
Keys
.
NumPad
1
)

DimNumPad
2
AsString
=
GetAsyncKeyState
(
Keys
.
NumPad
2
)

DimNumPad
3
AsString
=
GetAsyncKeyState
(
Keys
.
NumPad
3
)

DimNumPad
4
AsString
=
GetAsyncKeyState
(
Keys
.
NumPad
4
)

DimNumPad
5
AsString
=
GetAsyncKeyState
(
Keys
.
NumPad
5
)

DimNumPad
6
AsString
=
GetAsyncKeyState
(
Keys
.
NumPad
6
)

DimNumPad
7
AsString
=
GetAsyncKeyState
(
Keys
.
NumPad
7
)

DimNumPad
8
AsString
=
GetAsyncKeyState
(
Keys
.
NumPad
8
)

DimNumPad
9
AsString
=
GetAsyncKeyState
(
Keys
.
NumPad
9
)

'
Digits

DimKey
0
AsString
=
GetAsyncKeyState
(
Keys
.
D
0
)

DimKey
1
AsString
=
GetAsyncKeyState
(
Keys
.
D
1
)

DimKey
2
AsString
=
GetAsyncKeyState
(
Keys
.
D
2
)

DimKey
3
AsString
=
GetAsyncKeyState
(
Keys
.
D
3
)

DimKey
4
AsString
=
GetAsyncKeyState
(
Keys
.
D
4
)

DimKey
5
AsString
=
GetAsyncKeyState
(
Keys
.
D
5
)

DimKey
6
AsString
=
GetAsyncKeyState
(
Keys
.
D
6
)

DimKey
7
AsString
=
GetAsyncKeyState
(
Keys
.
D
7
)

DimKey
8
AsString
=
GetAsyncKeyState
(
Keys
.
D
8
)

DimKey
9
AsString
=
GetAsyncKeyState
(
Keys
.
D
9
)

'
Operations

DimOperator
_
PlusAsString
=
GetAsyncKeyState
(
Keys
.
Add
)

DimOperator
_
MinusAsString
=
GetAsyncKeyState
(
Keys
.
Subtract
)

DimOperator
_
MultiplyAsString
=
GetAsyncKeyState
(
Keys
.
Multiply
)

DimOperator
_
DivideAsString
=
GetAsyncKeyState
(
Keys
.
Divide
)

'
DotandEnter

DimKeyDotAsString
=
GetAsyncKeyState
(
Keys
.
OemPeriod
)

DimKeyDecimalAsString
=
GetAsyncKeyState
(
Keys
.
Decimal
)

DimKeyEnterAsString
=
GetAsyncKeyState
(
Keys
.
Enter
)

'
Fornumbers

IfNumPad
0
=
TrueOrKey
0
=
TrueThen

MessageBox
.
Show
("
Youhavepressed
0
.")

ElseIfNumPad
1
=
TrueOrKey
1
=
TrueThen

MessageBox
.
Show
("
Youhavepressed
1
.")

ElseIfNumPad
2
=
TrueOrKey
2
=
TrueThen

MessageBox
.
Show
("
Youhavepressed
2
.")

ElseIfNumPad
3
=
TrueOrKey
3
=
TrueThen

MessageBox
.
Show
("
Youhavepressed
3
.")

ElseIfNumPad
4
=
TrueOrKey
4
=
TrueThen

MessageBox
.
Show
("
Youhavepressed
4
.")

ElseIfNumPad
5
=
TrueOrKey
5
=
TrueThen


MessageBox
.
Show
("
Youhavepressed
5
.")

ElseIfNumPad
6
=
TrueOrKey
6
=
TrueThen

MessageBox
.
Show
("
Youhavepressed
6
.")

ElseIfNumPad
7
=
TrueOrKey
7
=
TrueThen

MessageBox
.
Show
("
Youhavepressed
7
.")

ElseIfNumPad
8
=
TrueOrKey
8
=
TrueThen

MessageBox
.
Show
("
Youhavepressed
8
.")

ElseIfNumPad
9
=
TrueOrKey
9
=
TrueThen

MessageBox
.
Show
("
Youhavepressed
9
.")

EndIf

'
Foroperators

IfOperator
_
Plus
=
TrueThen

MessageBox
.
Show
("
Youhavepressed
'
+
'
.")

ElseIfOperator
_
Minus
=
TrueThen

MessageBox
.
Show
("
Youhavepressed
'

'
.")

ElseIfOperator
_
Multiply
=
TrueThen

MessageBox
.
Show
("
Youhavepressed
'
*
'
.")

ElseIfOperator
_
Divide
=
TrueThen

MessageBox
.
Show
("
Youhavepressed
'
/
'
.")

EndIf

'
Fordotandenter

IfKeyDot
=
TrueOrKeyDecimal
=
TrueThen

MessageBox
.
Show
("
Youhavepressed
'
.
'
.")

ElseIfKeyEnter
=
TrueThen

MessageBox
.
Show
("
Youhavepressed
'
enter
'
.")

EndIf

EndSub
EndClass

You might also like