Jump to content

Need expert scripting advice


Recommended Posts

I found a script here in forums, IsPressed Function but kind a confused with some numbers. need some expert advice

 

Func _IsPressed($hexKey)

    Local $aR, $bO

    $hexKey = '0x' & $hexKey
    $aR = DllCall("user32", "int", "GetAsyncKeyState", "int", $hexKey)
    If Not @error And BitAND($aR[0], 0x8000) = 0x8000 Then ;whats the meaning of 0x and 0x8000??? where did he get this? is there any reference for this???
        $bO = 1
    Else
        $bO = 0
    EndIf

    Return $bO
EndFunc   ;==>_IsPressed

 

ill get to that... i still need to learn and understand a lot of codes graduated.gif

Correct answer, learn to walk before you take on that marathon.

Link to comment
Share on other sites

0x8000 is a hex number and what it is for is to strip the high bytes off of the number returned from the DLLCall so all you get back is the low bytes.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

BTW: Could you please give meaningful titles to your threads? Because everyone is seeking for some kind of advice on this forum ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I may misunderstand Brewman's words here, because he strikes me as a man who knows more about this, but this operation doesn't "strip off high bytes and returns low bytes"...

First, it's a "high bit " (not the high byte) we're talking about, or better worded IMHO the most significant bit of the SHORT that GetAsyncKeyState returns. (See here for the msdn doc, and let's stay big-endian for the moment :) )

Second, this method "strips off" (though I disagree with that terminology, which may be the misunderstanding?) low bits, not high bits. What BitAND does is comparing two numbers by writing them out in binary and returning the number you get when you "and" all the bits, i.e. each binary digit in the result is only 1 when that digit was also 1 in both input numbers. When you write hexadecimal number 0x8000 in binary, you get: 1000 0000 0000 0000. Note that there is only one 1 in that number, which meaning that if you BitAND() that number with any other number, the result ends up being either 0x8000 or 0x0 (which is just 0). Because for every digit except the leftmost one, at least one of the two digits of the compared numbers is 0, so the and-value is also 0. The leftmost one is either 0 or 1: it is certainly 1 in our second comparison number (0x8000), and will be 1 in the result number if and only if it was also 1 in the first comparison number.

In this case, the return value of the function has its most significant bit set to 1 if the keycode you gave the function as a parameter is down, and to 0 if it is up. (Or if there was a problem in the method, in which case the entire result value is 0 in total.)

/edit: See the keycodes, and again, the MSDN doc of the GetAsyncKeyState function.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

You are correct SadBunny, read it too fast and was comparing it in my head with another function instead of what this one was doing.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...