AJ | Pact | logo
PACT
Supercollider a day

Supercollider a day

1 September 2009

Hackpact (30 posts)

For Hackpact I will attempt to learn the Supercollider audio programming language, updating this page with a new block of code each day.

ASCII keyboard testing

27/09/09

ASCII keyboard testing.

/* ==========================================================================
   Supercollider a day - for HackPact 2009
   -------------------
   by Adam Jansch
  
   27 September 2009
   
   ASCII keyboard testing
   ========================================================================== */


 
(
  var keyboard_test_window, keyboard_test_label, key_down_state;
  
  keyboard_test_window = SCWindow("Keyboard test", Rect(100, 100, 300, 100)).front;
  keyboard_test_label = SCStaticText(keyboard_test_window, Rect(10, 10, 280, 80));
  
  keyboard_test_label.font = Font("Arial", 42);
  keyboard_test_label.string = "B";
  
  key_down_state = 0;
  
  keyboard_test_window.view.keyDownAction = {
    |view, char, modifiers, unicode, keycode|
    
    // Stops repeating key down message

    if(key_down_state == 0,
    {
      ["Key down", char, keycode].postln;
      
      keyboard_test_label.string = [char, keycode, unicode];
      
      key_down_state = 1;
    });
  };
  
  keyboard_test_window.view.keyUpAction = {
    |view, char, modifiers, unicode, keycode|
    
    ["Key up", char, keycode].postln;
    
    keyboard_test_label.string = "";
    
    // Reset key down state flag

    key_down_state = 0;
  };
)