Jump to content

Keypress detection.


Go to solution Solved by orbs,

Recommended Posts

Hello all!

I was looking for a solution to a question I had and wondered if any of you wonderful people could assist me.

I am wondering if it would be possible to simply determine when a user has pressed any key at all on a keyboard. I do not care about specific keys, but I want to know if such a thing would be possible to do without listening for every key on the keyboard, (which would be bad, because that would be like a keylogger, which is nothing at all like what I want, and is in fact against the rules of the forums as you probably already know), and how I might go about doing this. (I realize that there are some keys that are impossible to detect, such as the Fn key found on many laptops.)

So, to reiterate; I just want to know when any key at all is pressed. The key that is pressed isn't important at all. (In fact, a generic approach would be ideal to reduce complexity of the code.)

Thanks for your help!

Link to comment
Share on other sites

I am sure there was some "anykey" in the WinAPI function, but can't think of it now.

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

hello TheBytemaster, and welcome to AutoIt and to the forum.

AutoIt has a function that will let you detect input for keyboard and mouse (and probably any other HID). look for:

_Timer_GetIdleTime()

but if it's only keyboard you are interested in, that a bit more complicated, i'll let the API/DLL gurus around take on it.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

Well if you suggest that then there is _WinAPI_GetIdleTime().

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

 

Something like this?

#include <WindowsConstants.au3>
 
GUIRegisterMsg($WM_KEYDOWN, "IsPressed")
 
; do anything here
 
Func IsPressed()
    MsgBox(4096, "Test", "Key is pressed!", 2)
EndFunc

 

by MSDN: "Posted to the window with the keyboard focus". that's only for your GUI, so you can't detect keys sent to other windows with this method, right?

also, you'll have to register the WM_SYSKEYDOWN message, and still you'll probably miss some special keys.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

by MSDN: "Posted to the window with the keyboard focus". that's only for your GUI, so you can't detect keys sent to other windows with this method, right?

also, you'll have to register the WM_SYSKEYDOWN message, and still you'll probably miss some special keys.

 

Aha, correct!

This will only detect keys for our own GUI and will not detect special keys.

Why OP want to detect any keys on ANY GUIs while he state it's nothing to do with keylogger?

Link to comment
Share on other sites

@michaelslamet, as far as i can read hes trying to detect idle keyboard with no mouse, what does that have to do with keylogger?

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

  • Solution

this seems a bit familiar to an issue i had in the past, only in reverse:

'?do=embed' frameborder='0' data-embedContent>>

so venturing into gedankenexperiment territory, i'd check the idle time. but if idle time reports activity, i'd check also for mouse moves, and assume a key press only if mouse did not move.

anyway i think it's time to let the OP get the reins now.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

this seems a bit familiar to an issue i had in the past, only in reverse:

'?do=embed' frameborder='0' data-embedContent>>

so venturing into gedankenexperiment territory, i'd check the idle time. but if idle time reports activity, i'd check also for mouse moves, and assume a key press only if mouse did not move.

anyway i think it's time to let the OP get the reins now.

Ah, thank you. I believe that will do nicely. Thanks so much for your help!

 

Something like this?

#include <WindowsConstants.au3>
 
GUIRegisterMsg($WM_KEYDOWN, "IsPressed")
 
; do anything here
 
Func IsPressed()
    MsgBox(4096, "Test", "Key is pressed!", 2)
EndFunc

That's not what I was looking for as the others have stated, but $WM_KEYDOWN seems useful, and I might just use that in a later project I was brainstorming. I'll have to remember that one. Thanks!

(I didn't reply earlier because for some reason I wasn't getting any alerts for responses to my thread. I was actually kind of sad that I hadn't gotten any responses as far as I could tell, and I was about to try and figure out if bumping was acceptable here. I promise I'm not normally lazy with responses when people are kind enough to be helping me.) :P

Link to comment
Share on other sites

  • 3 years later...

From what I can tell, the _Timer_GetIdleTime function is reset when on wireless connection by the wireless connection itself. 

Is there a method to prevent  this from happening?

the following code closes IE or Edge after a set idle time.  It adds a countdown timer for a warning as well.  When on a wired network everything works as expected, however when moved to a wireless connection (windows 10 machine) the idle time appears to be reset.
 
Has anyone come across this issue? 
Any thoughts on how to get around it?
I was going to check the keyboard for a keystroke and then the mouse for movement however it appears from this thread that there isn't a method for determining any key stroke.  If there is a way to ignore the reset apparently coming from the wireless?
 
$timeLimit = Int(IniRead("config.ini","limit","time","Default Value"))
$cdTimer = Int(IniRead("config.ini","limit","cdtime","Default Value"))
 
 
local $miliseconds =  1000
local $countDownSeconds =  ($cdTimer  * $miliseconds)
local $fiveSeconds =  (5   * $miliseconds)
 
; time in miliseconds since an imput was registered on the system
Local $iIdleTime = _Timer_GetIdleTime()

while 1 ;run the script continuously in the background
 Sleep($fiveSeconds) ; used to reduce system drain
 $iIdleTime = _Timer_GetIdleTime()
  while $iIdleTime < $timeLimit
   Sleep($fiveSeconds) ; used to reduce system drain
   $iIdleTime = _Timer_GetIdleTime()
   if ($iIdleTime > $timeLimit - $countDownSeconds and ProcessExists("IEXPLORE.EXE") > 0) Or ($iIdleTime > $timeLimit - $countDownSeconds and ProcessExists("MicrosoftEdge.EXE") > 0) Then
    MESSAGE($countDownSeconds)
   EndIf
   $iIdleTime = _Timer_GetIdleTime()
  WEnd
 ; Clear all temp files in IE and close all instances of IE
 If ProcessExists("IEXPLORE.exe") > 0  then
 ; Clear IE history and all temporary files
  $ClearID = "255"
  RunWait("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess " & $ClearID)
 ; Close all instamces of IE
  While ProcessExists("IEXPLORE.EXE") > 0
   RunWait('taskkill /F /IM "iexplore.exe"')
  WEnd
 EndIf
 ; close all instances of Edge
 If ProcessExists("MicrosoftEdge.exe") > 0  then
  While ProcessExists("MicrosoftEdge.exe") > 0
   RunWait('taskkill /F /IM "MicrosoftEdge.exe"')
  WEnd
 EndIf
 
WEnd
 
Func Message($countDownSeconds)
 $MsgBoxMiliSeconds = $countDownSeconds + 1000 ; Countdown timer length in miliseconds
 $timer = TimerInit()
 $lableText = "Your internet session will be end in           seconds."& @CRLF& @CRLF & "Move mouse to continue."
 GUICreate("Browser Close",350,60,-1,-1,-1,-1)
   $Lable = GUICtrlCreateLabel($lableText,10,10,330,200)
   ;$Input = GUICtrlCreateInput("", 140, 225, 75, 20, BitOr(0x001, 0x0800))
   $Input = GUICtrlCreateInput("", 175, 5, 25, 20, BitOr(0x001, 0x0800))
   GUISetState (@SW_SHOW)
 While (TimerDiff($timer)<$MsgBoxMiliSeconds)
   ; use the imput timer as exit for loop
   If (_Timer_GetIdleTime() < ($timeLimit - $thirtySeconds)) then
    ExitLoop
   EndIf
   ;set up for countdown timer on GUI
   $seconds = TimerDiff($timer)/1000
   $diff = $seconds - ($MsgBoxMiliSeconds/1000)
   $secondsRem = $diff * - 1
   $time = StringFormat("%02d",$secondsRem)

   GUICtrlSetData($input,$time)
 WEnd
  GUIDelete();    ; will return 1 (removes GUI from the screen
EndFunc
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...