Disable Excel key-catcher

pacman46

New Member
Joined
Dec 1, 2011
Messages
2
Hi everyone !
I'm actually trying to make a game in Excel, coded in VBS (I know, this is crazy ), and I use the API "GetAsyncKeyState" in order to catch what keys are pressed :

Code:
Public Const VK_UP = &H26
Public Const VK_DOWN = &H28
Public Const VK_LEFT = &H25
Public Const VK_RIGHT = &H27
Public Const VK_DELETE = &H2E

Set oDyn = CreateObject("DynamicWrapper")
oDyn.Register "User32.dll", "GetAsyncKeyState", "i=l", "r=t"

Set ExcelApp = WScript.CreateObject("EXCEL.application")
ExcelApp.Visible = True
ExcelApp.workbooks.Add
ExcelApp.sheets(1).Activate
ExcelApp.DataEntryMode = True

WScript.Echo "Debut"
continue = True
While (continue)
    If (oDyn.GetAsyncKeyState(VK_UP) = -32767) Then
        WScript.Echo "UP"
    End If
    If (oDyn.GetAsyncKeyState(VK_DOWN) = -32767) Then
        WScript.Echo "DOWN"
    End If
    If (oDyn.GetAsyncKeyState(VK_LEFT) = -32767) Then
        WScript.Echo "LEFT"
    End If
    If (oDyn.GetAsyncKeyState(VK_RIGHT) = -32767) Then
        WScript.Echo "RIGHT"
    End If
    If (oDyn.GetAsyncKeyState(VK_DELETE) = -32767) Then
        continue = False
    End If
    WScript.Sleep(50)
Wend

Set oDyn = Nothing
ExcelApp.DisplayAlerts = False
ExcelApp.Quit
ExcelApp.DisplayAlerts = True
Set ExcelApp = Nothing
It works perfectly, except when the Excel window is activated (i.e. foreground) : GetAsyncKeyState doesn't catch keys anymore. Excel seems to keep it for himself, and don't share it with the System...

I already tried to disable the Excel DataEntryMode, but my problem's still here...
Is there a way to disable this Excel's stupid behaviour, or making GetAsyncKeyState work better ?

Thanks for all your replies.

pacman
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Nvm, I got the solution to my problem :
When I check the GetAsyncKetState return value, I compare it with -32767. It permits to limit the repetition frequency of the key, which is great. But if you juste make that :
Code:
    If (oDyn.GetAsyncKeyState(VK_UP) <> 0) Then
        WScript.Echo "UP"
    End If

The problem encountered with Excel doesn't appear anymore.
I also raised the
Code:
WScript.Sleep(50)
to 70ms.
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,500
Members
449,090
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top