+ Reply to Thread
Results 1 to 15 of 15

Function GetAsyncKeyState Lib "user32.dll" .. Stops Working

  1. #1
    Registered User
    Join Date
    05-19-2023
    Location
    NJ, US
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2304 Build 16.0.16327.20200) 64-bit
    Posts
    5

    Function GetAsyncKeyState Lib "user32.dll" .. Stops Working

    Hello All.

    I have an excel workbook with a bunch of sheets. I am using the Function GetAsyncKeyState Lib "user32.dll" ... and it works fine till I Freeze or Unfreeze a sheet. The other sheets still work and work till I Unfreeze the sheet. If I close the workbook and reopen it function works till I Freeze/Unfreeze the sheet.

    Any ideas..

    Thank you,
    Mark
    Last edited by markw1230; 05-19-2023 at 01:20 PM.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: Function GetAsyncKeyState Lib "user32.dll" .. Stops Working

    What exactly do you mean by "stops working", and what do you mean by freezing a worksheet (freeze panes?)?
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    05-19-2023
    Location
    NJ, US
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2304 Build 16.0.16327.20200) 64-bit
    Posts
    5

    Re: Function GetAsyncKeyState Lib "user32.dll" .. Stops Working

    Rory,

    I am using "user32.dll" to check for a mouse click that works till I freeze or unfreeze a pane in the workbook. If I close the workbook and reopen it it works again. I have been searching high and low and can not find a fix. Below if a peak of the code I am uning.

    Module1:

    Option Explicit

    Public Declare PtrSafe Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Integer


    ThisWorkbook:

    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

    Dim vLeftButton As Boolean

    vLeftButton = GetAsyncKeyState(VK_LBUTTON) <> 0


    Thank you,
    Mark

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: Function GetAsyncKeyState Lib "user32.dll" .. Stops Working

    OK and what precisely is happening with the code now? In what way is it not working after you freeze panes?

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Function GetAsyncKeyState Lib "user32.dll" .. Stops Working

    Should that be with no PtrSafe in 32
    Please Login or Register  to view this content.
    and
    and 64 something like
    Please Login or Register  to view this content.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: Function GetAsyncKeyState Lib "user32.dll" .. Stops Working

    Hi Pike!

    In a word, no. The declaration is correct as it is. (there's no such thing as IntPtr anyway)

  7. #7
    Registered User
    Join Date
    05-19-2023
    Location
    NJ, US
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2304 Build 16.0.16327.20200) 64-bit
    Posts
    5

    Re: Function GetAsyncKeyState Lib "user32.dll" .. Stops Working

    Quote Originally Posted by rorya View Post
    OK and what precisely is happening with the code now? In what way is it not working after you freeze panes?
    Rorya,

    GetAsyncKeyState(VK_LBUTTON) always returns "0" after I freeze or unfreeze a pane.

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Function GetAsyncKeyState Lib "user32.dll" .. Stops Working


    Hi,

    as this is very not an Excel feature, maybe more chance on a Windows forum …

    Anyway try GetKeyState rather than GetAsyncKeyState.

  9. #9
    Registered User
    Join Date
    05-19-2023
    Location
    NJ, US
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2304 Build 16.0.16327.20200) 64-bit
    Posts
    5

    Re: Function GetAsyncKeyState Lib "user32.dll" .. Stops Working

    Quote Originally Posted by Marc L View Post

    Hi,

    as this is very not an Excel feature, maybe more chance on a Windows forum …

    Anyway try GetKeyState rather than GetAsyncKeyState.
    I just gave the above a try and if catches every other left mouse key till I freeze or unfreeze the pan, then stops working again.

    Thanks, Mark

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Function GetAsyncKeyState Lib "user32.dll" .. Stops Working


    So no other option than to not freeze / unfreeze any pan …

    If you can test on a previous Excel version, different than 365, just to be sure it's a normal behaviour and not specific to 365.

  11. #11
    Registered User
    Join Date
    05-19-2023
    Location
    NJ, US
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2304 Build 16.0.16327.20200) 64-bit
    Posts
    5

    Re: Function GetAsyncKeyState Lib "user32.dll" .. Stops Working

    Can someone please try my stripped down sample code below and let me know if you get the same results as I do. I have been trying to get this to work and can not find the problem.
    After setting up the code just click anywhere on the sheet to get started.

    vGetAsuncKeyState and vGetkeyState should be True on every left mouse click.

    Thank you for your help. Mark

    Create Module1 and use code below:
    -------------------------------------------------------------------------------------------------------
    Option Explicit

    Public Declare PtrSafe Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Integer

    Public Declare PtrSafe Function GetKeyState Lib "user32.dll" (ByVal vKey As Long) As Integer

    Public Const VK_LBUTTON = &H1
    Public Const VK_RBUTTON = &H2
    --------------------------------------------------------------------------------------------------------

    Place in ThisWorkbook and use code below:
    --------------------------------------------------------------------------------------------------------
    Option Explicit

    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

    Dim vGetAsyncKeyState As Boolean
    Dim vGetKeyState As Boolean

    vGetAsyncKeyState = GetAsyncKeyState(VK_LBUTTON) <> 0
    vGetKeyState = GetKeyState(VK_LBUTTON) <> 0

    Range("A1").Value = "vGetAsyncKeyState = " & vGetAsyncKeyState
    Range("A2").Value = "vGetKeyState = " & vGetKeyState

    If IsEmpty(Range("A4")) Then ' Set up cells for display
    Range("A4").Value = "Click to freeze pane"
    Range("A4").Font.Underline = True
    Columns("A:A").ColumnWidth = 25
    Columns("B:B").ColumnWidth = 60
    Range("B1").Value = "<-- Should be True on every left mouse click on a different cell."
    Range("B2").Value = "<-- Should be True on every left mouse click on a different cell."
    Range("B7").Value = "vGetAsyncKeyState works till you freeze the pane."
    Range("B8").Value = "vGetKeyState works on every other mouse click."
    End If

    If Target.Row = "4" And Target.Column = "1" And Range("A4").Value = "Click to freeze pane" Then 'A1

    Range("A4").Value = "Click to unfreeze pane"
    Range("A5").Select
    ActiveWindow.FreezePanes = True

    ElseIf Target.Row = "4" And Target.Column = "1" And Range("A4").Value = "Click to unfreeze pane" Then 'A1

    Range("A4").Value = "Click to freeze pane"
    ActiveWindow.FreezePanes = False

    End If

    End Sub
    -----------------------------------------------------------------------------------------------------------------

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: Function GetAsyncKeyState Lib "user32.dll" .. Stops Working

    Yes, same result here (also 64 bit 365). It works on every other click once the panes are frozen

  13. #13
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Function GetAsyncKeyState Lib "user32.dll" .. Stops Working

    Hey Rory!!
    I was looking at the OP using the syntax
    Please Login or Register  to view this content.
    in 32bit. Isn't PtrSafe a 64bit attribute?
    Yes you're right the other declarations aren't integrals , pointers or handles so don't need to be LongLong or longptr in 64bit , I find it hard to decipher which to change to and which ones to leave .. yes i did make intptr up on the fly...

  14. #14
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: Function GetAsyncKeyState Lib "user32.dll" .. Stops Working

    PtrSafe was introduced with VBA7 and works regardless of bitness. As long as you don't need to worry about Excel 2007 or earlier, PtrSafe is fine; if you do, then you need conditional compilation for #If VBA7 then as well

  15. #15
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Function GetAsyncKeyState Lib "user32.dll" .. Stops Working

    Know the op's pain ... crashed the application a few times with API's

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Issue with "IF" function when working with "N/A" function
    By Timat124 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-19-2020, 03:59 AM
  2. VBA "Replace" code stops working after every 200-215 loops
    By drflgd in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-09-2019, 02:23 PM
  3. Replies: 5
    Last Post: 02-10-2016, 07:20 PM
  4. [SOLVED] What stops Range("B:HZ").EntireColumn.Hidden = True working
    By Tiger20 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-08-2015, 08:03 AM
  5. Macro. When "If" function right mcaro stops
    By marte13 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-17-2014, 02:28 PM
  6. Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)" not working
    By redders in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2011, 03:52 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1