# Detect whether a key is being held before script execution?

**URL:** <https://www.ruby-forum.com/t/detect-whether-a-key-is-being-held-before-script-execution/186893>\
**Category:** Ruby\
**Created:** [April 13, 2010, 10:25pm UTC](https://www.ruby-forum.com/t/detect-whether-a-key-is-being-held-before-script-execution/186893 "2010-04-13T22:25:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@wet](https://www.ruby-forum.com/u/wet)\
**Post date:** [April 13, 2010, 10:25pm UTC](https://www.ruby-forum.com/t/detect-whether-a-key-is-being-held-before-script-execution/186893/1 "2010-04-13T22:25:13Z")

</div>

Hi All!

I am looking for a way to have a script detect whether or not a key is  
pressed and held down before it runs.

Basically, I want the script to execute one way if the key is pressed  
and another if it is not pressed.

I plan on having the script launched automatically at startup and will  
need to have the key pressed before it runs.

Can anybody point me in the right direction to achieve this?

Thanks in advance!

WPPK

---

<div class="post-metadata">

**Author:** ![wet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@wet](https://www.ruby-forum.com/u/wet)\
**Post date:** [April 13, 2010, 10:36pm UTC](https://www.ruby-forum.com/t/detect-whether-a-key-is-being-held-before-script-execution/186893/2 "2010-04-13T22:36:29Z")

</div>

On Tue, Apr 13, 2010 at 3:25 PM, Thomas W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi All!
> 
> I am looking for a way to have a script detect whether or not a key is  
> pressed and held down before it runs.  
> Is this within the context of a web browser, command line interface,  
> or gui (like windows start up)?

Your best bet is to automatically launch your first script which will  
watch for user input, then conditionally launch the next part.  
Without more info, I am not entirely sure what you are trying to  
accomplish.

Andrew McElroy

---

<div class="post-metadata">

**Author:** ![wet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@wet](https://www.ruby-forum.com/u/wet)\
**Post date:** [April 13, 2010, 10:46pm UTC](https://www.ruby-forum.com/t/detect-whether-a-key-is-being-held-before-script-execution/186893/3 "2010-04-13T22:46:01Z")

</div>

Command Line. I want to have the script launched at startup, by having a  
Terminal command call the script, then when the script executes, it  
determines whether or not the key is being held down and if it is, it  
then modifies the permissions of certain folders on my hard disk.

I am using OSX.

Andrew Mcelroy wrote:

> On Tue, Apr 13, 2010 at 3:25 PM, Thomas W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Hi All!
> > 
> > I am looking for a way to have a script detect whether or not a key is  
> > pressed and held down before it runs.  
> > Is this within the context of a web browser, command line interface,  
> > or gui (like windows start up)?
> 
> Your best bet is to automatically launch your first script which will  
> watch for user input, then conditionally launch the next part.  
> Without more info, I am not entirely sure what you are trying to  
> accomplish.
> 
> Andrew McElroy

---

<div class="post-metadata">

**Author:** ![wet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@wet](https://www.ruby-forum.com/u/wet)\
**Post date:** [April 14, 2010, 3:43am UTC](https://www.ruby-forum.com/t/detect-whether-a-key-is-being-held-before-script-execution/186893/4 "2010-04-14T03:43:20Z")

</div>

Could anybody possibly give me a code example of how to detect a  
keyboard event?  
I am new to Ruby and I am not quite familiar with the syntax yet.  
Thanks.

Thomas W. wrote:

> Command Line. I want to have the script launched at startup, by having a  
> Terminal command call the script, then when the script executes, it  
> determines whether or not the key is being held down and if it is, it  
> then modifies the permissions of certain folders on my hard disk.
> 
> I am using OSX.
> 
> Andrew Mcelroy wrote:
> 
> > On Tue, Apr 13, 2010 at 3:25 PM, Thomas W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > wrote:
> > 
> > > Hi All!
> > > 
> > > I am looking for a way to have a script detect whether or not a key is  
> > > pressed and held down before it runs.  
> > > Is this within the context of a web browser, command line interface,  
> > > or gui (like windows start up)?
> > 
> > Your best bet is to automatically launch your first script which will  
> > watch for user input, then conditionally launch the next part.  
> > Without more info, I am not entirely sure what you are trying to  
> > accomplish.
> > 
> > Andrew McElroy
