Jump to content

Count how much time key is pressed


xdp22
 Share

Recommended Posts

Hello i need to do counter how much time key is pressed, i have that work to ONE KEY, but need to do that with 5 keys at one time. Trust me i try to do that many hours, but i can't do it, for one key i have that :

While 1
_KeyPressTime('41')
WEnd


Func _KeyPressTime($key)
While 1
If _IsPressed($key) = 1 Then
 $Timer = TimerInit()
 Do
 ;
 Until _IsPressed($key) = 0
 If _IsPressed($key) = 0 Then
 MsgBox( 0, "", TimerDiff($Timer))
 EndIf
EndIf
WEnd
EndFunc ;==>_KeyPressTime

I many other tries, but i can't make it works, please help me, i need make something like - Push Key A, and after release the key get time how much key is pressed.

:)

I trying like that

#include <timers.au3>
#include <file.au3>
#include <ispressed.au3>

While 1
AdlibRegister( "a", 1)
AdlibRegister( "w", 1)
WEnd

Func a()
_KeyPressTime('41')
EndFunc

Func w()
_KeyPressTime('57')
EndFunc


Func _KeyPressTime($key)
While 1
If _IsPressed($key) = 1 Then
 $Timer = TimerInit()
 Do
 ;
 Until _IsPressed($key) = 0
 If _IsPressed($key) = 0 Then
FileWriteLine( "keys.txt", $key & " /  " & TimerDiff($Timer))
 EndIf
EndIf
WEnd
EndFunc ;==>_KeyPressTime

But when i hold one key, and push another, it will count time only for this first what i hold :/ i need to count more keys at one time, please help ;)

Edited by xdp22
Link to comment
Share on other sites

OK, you changed your original post to many times!

#include <Misc.au3>

Global $Count = 0, $Timer

While 1
    If _IsPressed("41") = 1 Then
        $Count += 1
        $Timer = TimerInit()
        While _IsPressed("41")
            Sleep(100)
        WEnd
        ConsoleWrite("A has been Pressed " & $Count & " times(s)" & @CRLF)
        MsgBox(0, "A Was Pressed For ... ", TimerDiff($Timer) & " ms")
    EndIf
WEnd

Just saw your Update >>

#include <Misc.au3>

Global $Count = 0, $Return

While 1
    If _IsPressed("41") Then
        $Return = _KeyPressed("41", $Count)
        MsgBox(0, "A Was Pressed For ... ", $Return & " ms")
        ConsoleWrite("A has been Pressed " & $Count & " times(s)" & @CRLF)
    EndIf
WEnd

Func _KeyPressed($kp_Key, ByRef $kp_Count)
    $kp_Count += 1
    Local $kp_Timer = TimerInit()
    While _IsPressed("41")
        Sleep(100)
    WEnd
    Return TimerDiff($kp_Timer)
EndFunc   ;==>_KeyPressed
Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Thanks for reply, so i make :

#include <Misc.au3>

Global $Count = 0, $Return

While 1
    If _IsPressed("41") Then
        $Return = _KeyPressed("41", $Count)
        MsgBox(0, "A Was Pressed For ... ", $Return & " ms")
        ConsoleWrite("A has been Pressed " & $Count & " times(s)" & @CRLF)
    EndIf
        If _IsPressed("57") Then
        $Return = _KeyPressed("57", $Count)
        MsgBox(0, "W Was Pressed For ... ", $Return & " ms")
        ConsoleWrite("W has been Pressed " & $Count & " times(s)" & @CRLF)
    EndIf
WEnd

Func _KeyPressed($kp_Key, ByRef $kp_Count)
    $kp_Count += 1
    Local $kp_Timer = TimerInit()
    While _IsPressed($kp_Key)
        Sleep(100)
    WEnd
    Return TimerDiff($kp_Timer)
EndFunc   ;==>_KeyPressed

And hold W and A at this same time, but return time only of one key :/ this what i click first :/

Edited by xdp22
Link to comment
Share on other sites

OK, So I just saw your other post and you want to log only two keys! Are you sure this isn't for malicious purposes? I did this in the mean time, as the initial post has changed a few times since I started helping. :)

#include <Misc.au3>

Global $Count = 0, $Return
Global $IsPressed[6][2] = [[5, 0],[41, 0],[42, 0],[43, 0],[44, 0],[45, 0]] ; <<<<< Column 1 Key & Column 2 Count
While 1
    $Return = _KeyPressed($IsPressed)
    If Not @error Then
        $Count = @extended
        MsgBox(0, $IsPressed[$Count][0] & " Was Pressed For ... ", $Return & " ms")
        ConsoleWrite($IsPressed[$Count][0] & " has been Pressed " & $IsPressed[$Count][1] & " times(s)" & @CRLF)
    EndIf

WEnd

Func _KeyPressed(ByRef $kp_Keys)
    For $A = 1 To $kp_Keys[0][0]
        If _IsPressed($kp_Keys[$A][0]) Then ExitLoop
    Next
    If $A = $kp_Keys[0][0] + 1 Then Return SetError(1, 1, 0)
    $kp_Keys[$A][1] += 1
    Local $kp_Timer = TimerInit()
    While _IsPressed($kp_Keys[$A][0])
        Sleep(100)
    WEnd
    Local $Diff = TimerDiff($kp_Timer)
    Return SetError(0, $A, $Diff)
EndFunc   ;==>_KeyPressed
Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Sry for bad english i hope u can understand what i mean.

Thanks for try to help me but doesn't work for me

#include <Misc.au3>

Global $Count = 0, $Return
Global $IsPressed[6][2] = [[5, 0],[41, 0],[57, 0],[43, 0],[44, 0],[45, 0]] ; <<<<< Column 1 Key & Column 2 Count
While 1
    $Return = _KeyPressed($IsPressed)
    If Not @error Then
        $Count = @extended
        MsgBox(0, "A Was Pressed For ... ", $Return & " ms")
        ConsoleWrite("A has been Pressed " & $IsPressed[$Count][1] & " times(s)" & @CRLF)
    EndIf

WEnd

Func _KeyPressed(ByRef $kp_Keys)
    For $A = 1 To $kp_Keys[0][0]
        If _IsPressed($kp_Keys[$A][0]) Then ExitLoop
    Next
    If $A = $kp_Keys[0][0] + 1 Then Return SetError(1, 1, 0)
    $kp_Keys[$A][1] += 1
    Local $kp_Timer = TimerInit()
    While _IsPressed($kp_Keys[$A][0])
        Sleep(100)
    WEnd
    Local $Diff = TimerDiff($kp_Timer)
    Return SetError(0, $A, $Diff)
EndFunc   ;==>_KeyPressed

When i hold A key, and push the W key then nothing happen, when release the A key it's return the time. But i need to it works with many keys at one time. So in example if i holding A key, and push the W key to little while ( still holding A key, but release the W key, it will return time of W key, and when i release the A key it will return the A time key.

I will be still try to made that, if u have time, and can help me, thanks very much :)

Link to comment
Share on other sites

It wouldn't! When I first saw your initial post is was only detecting A, then it moved to more keys and then to keys being pushed simultaneously. I think my last example should be enough to improve to reach your goal. Good Luck!

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

I believe that the standard keyboard is a serial device, which only sends one scan code at a time.

(That is why the old DOS Video games used separate key press for diagonal movements.)

The 'modifier keys' (shift, ctrl, alt, [etc.]) simply change the scan code of that letter.

I have been wrong before, though!

Link to comment
Share on other sites

I believe that the standard keyboard is a serial device, which only sends one scan code at a time.

(That is why the old DOS Video games used separate key press for diagonal movements.)

The 'modifier keys' (shift, ctrl, alt, [etc.]) simply change the scan code of that letter.

I have been wrong before, though!

It seems to work fine for me up to three keys at a time. Don't know where that limitation is imposed though.

This is what I used for three keys at a time:

#include <misc.au3>
Global $Pressed[256][2]
While 1
    For $a = 1 To 255
        If _IsPressed(Hex($a)) Then; See if a key is currently pressed
            If $Pressed[$a][0] <> 1 Then; If the key pressed wasn't pressed last check, create a timer for the key
                $Pressed[$a][0] = 1
                $Pressed[$a][1] = TimerInit()
            EndIf
        Else; See if a key is NOT pressed
            If $Pressed[$a][0] = 1 Then; If the key NOT pressed used to be pressed last check, calculate the time it was pressed
                $Pressed[$a][0] = 0
                ConsoleWrite(Chr($a) & ' (0x' & StringRight(Hex($a), 2) & ') was pressed for ' & TimerDiff($Pressed[$a][1]) & ' milliseconds' & @CRLF)
            EndIf
        EndIf
    Next
    Sleep(10);For CPU sake
WEnd
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...