<div dir="ltr">Hi, I'm a coursera's ChucK course student, in the lectures, Ge used HIDs to handle keyboard input, so this snippet should help (I'm commenting the code for clarity).<br><br><span style="font-family:courier new,monospace">// HID stands for human-interface-device, also this is an event,<br>
// see below for more info<br>Hid hid;<br><br>// This is a message, this will hold the event's (keypress) information.<br>HidMsg msg;<br><br>// We should define a device number since we would have multiple keyboards <br>
// attached to our computer, 0 would do on most cases but you can try 1, or 2 <br>// if that doesn't work.<br>0 => int device_number;<br><br>// We check if the device attach with ChucK was successful, otherwise the program <br>
// would end inmediately<br>if(!hid.openKeyboard(device_number)) me.exit();<br><br><<<"Keyboard:", <a href="http://hid.name">hid.name</a>(), " ready!">>>;<br><br>// We setup a simple sound chain for demonstration pruposes only<br>
BeeThree organ => JCRev rev => dac;<br>.05 => rev.mix;<br><br>// And repeat forever, see how the keyboard input is something that would happen <br>// *forever*, a keypress can occur anytime so we need to stay alert until a new <br>
// event comes in<br>while(true) {<br>    // Events are things that happens sometime, in ChucK this mains basically <br>    // hold ourselves until the event appears (thats why we wain for the event <br>    // ChucKing it to now)<br>
    hid => now;<br><br>    // When our wait finishes: check all the messages that came from the keyboard<br>    // using the recv function, and passing msg as the holder of the information<br>    while(hid.recv(msg)) {<br>
<br>        // we check if the message is a buttondown message, in that case:<br>        if(msg.isButtonDown()) {<br>            // We show the ascii value of the key pressed<br>            <<< "Button down:", msg.ascii >>>;<br>
<br>            // and make our generator sound, see how we check if the<br>            // frequency too much high we simply discard this message<br>            msg.ascii => Std.mtof => float freq;<br>            if(freq > 20000) continue;<br>
<br>            freq => organ.freq;<br>            1 => organ.noteOn;<br><br>            // this will make sure the sound will keep for a minimum amount of <br>            // time, otherwise, concurrent events may stop before even commencing<br>
            80::ms => now;<br>        } else {<br>            // If we're not in a buttondown event, just assume it's a key has been<br>            // left out and shut down the sound.<br>            <<< "Button up:", msg.ascii >>>;<br>
            1 => organ.noteOff;<br>        }<br>    }<br><br>}<br></span><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/12/5 Manuel Bärenz <span dir="ltr"><<a href="mailto:manuel@enigmage.de" target="_blank">manuel@enigmage.de</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div bgcolor="#FFFFFF" text="#000000">
    <div>Hi Julien,<br>
      <br>
      No, this is not the case. KBHits are only key down events. I tried
      to include several kb => now, but it didn't help.<br>
      See also this example here:<br>
      <a href="http://chuck.cs.princeton.edu/doc/examples/event/kb2.ck" target="_blank">http://chuck.cs.princeton.edu/doc/examples/event/kb2.ck</a><br>
      Apparently, the solution is emptying the event queue by calling
      kb.getchar() until kb.more() returns false:<div class="im"><br>
      <br>
      KBHit kb;<br>
      <br>
      me.sourceDir() + "/bum.wav" => string filename;<br>
      <br>
      adc => WvOut b => blackhole;<br>
      filename => b.wavFilename;<br>
      kb => now;<br></div>
      while(kb.more()) kb.getchar();<div class="im"><br>
      b.closeFile();<br>
      <br>
      SndBuf buf => dac;<br>
      filename => buf.read;<br>
      kb => now;<br>
      <br></div>
      The important line is "while(kb.more()) kb.getchar();",
      apparently. So I guess kb => now; doesn't wait at all if there
      are still unprocessed keys in the queue.<br>
      <br>
      Best, Manuel<br>
      <br>
      Am 05/12/13 12:18, schrieb Julien Saint-Martin:<br>
    </div><div><div class="h5">
    <blockquote type="cite">
      <div dir="ltr">
        <div>
          <div>
            <div>
              <div>Hi Manuel,<br>
                <br>
              </div>
              Maybe your problem is as follow:<br>
              For each key press there is two kb events: key down and
              key up.<br>
              <br>
            </div>
            A simple solution maybe to write "kb => now;" twice.<br>
            <br>
          </div>
          Cheers,<br>
        </div>
        Julien<br>
        <div>
          <div><br>
            <br>
            <div><br>
            </div>
          </div>
        </div>
      </div>
      <div class="gmail_extra"><br>
        <br>
        <div class="gmail_quote">2013/12/5 Manuel Bärenz <span dir="ltr"><<a href="mailto:manuel@enigmage.de" target="_blank">manuel@enigmage.de</a>></span><br>
          <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello
            again,<br>
            <br>
            I also wanted to demonstrate a simple sequencer prototype
            that lets me<br>
            record a file and then plays it back to me. I've come this
            far:<br>
            <br>
            KBHit kb;<br>
            <br>
            me.sourceDir() + "/bum.wav" => string filename;<br>
            <br>
            adc => WvOut b => blackhole;<br>
            filename => b.wavFilename;<br>
            kb => now;<br>
            b.closeFile();<br>
            <br>
            SndBuf buf => dac;<br>
            filename => buf.read;<br>
            kb => now;<br>
            <br>
            But strangely, the program doesn't wait for the second kb
            event, but<br>
            exits directly after the first key that I hit. Any ideas
            what I'm doing<br>
            wrong?<br>
            <br>
            Best, Manuel<br>
            _______________________________________________<br>
            chuck-users mailing list<br>
            <a href="mailto:chuck-users@lists.cs.princeton.edu" target="_blank">chuck-users@lists.cs.princeton.edu</a><br>
            <a href="https://lists.cs.princeton.edu/mailman/listinfo/chuck-users" target="_blank">https://lists.cs.princeton.edu/mailman/listinfo/chuck-users</a><br>
          </blockquote>
        </div>
        <br>
      </div>
      <br>
      <fieldset></fieldset>
      <br>
      <pre>_______________________________________________
chuck-users mailing list
<a href="mailto:chuck-users@lists.cs.princeton.edu" target="_blank">chuck-users@lists.cs.princeton.edu</a>
<a href="https://lists.cs.princeton.edu/mailman/listinfo/chuck-users" target="_blank">https://lists.cs.princeton.edu/mailman/listinfo/chuck-users</a>
</pre>
    </blockquote>
  </div></div></div>

<br>_______________________________________________<br>
chuck-users mailing list<br>
<a href="mailto:chuck-users@lists.cs.princeton.edu">chuck-users@lists.cs.princeton.edu</a><br>
<a href="https://lists.cs.princeton.edu/mailman/listinfo/chuck-users" target="_blank">https://lists.cs.princeton.edu/mailman/listinfo/chuck-users</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br>-Moisés
</div>