sensekey
Prev | Orchestra Opcodes and Operators | Next

## sensekey

sensekey — Returns the ASCII code of a key that has been pressed.

## Description

Returns the ASCII code of a key that has been pressed, or -1 if no key has been pressed.

## Syntax

    kres[, kkeydown] sensekey

## Performance

kres - returns the ASCII value of a key which is pressed or released.

kkeydown - returns 1 if the key was pressed, 0 if it was released or if there is no key event.

kres can be used to read keyboard events from stdin and returns the ASCII value of any key that is pressed or released, or it returns -1 when there is no keyboard activity. The value of kkeydown is 1 when a key was pressed, or 0 otherwise. This behavior is emulated by default, so a key release is generated immediately after every key press. To have full functionality, FLTK can be used to capture keyboard events. FLpanel can be used to capture keyboard events and send them to the sensekey opcode, by adding an additional optional argument. See FLpanel for more information.

| Note
This opcode can also be written as sense.

## Examples

Here is an example of the sensekey opcode. It uses the file sensekey.csd.

Example 953. Example of the sensekey opcode.

See the sections Real-time Audio and Command Line Flags for more information on using command line flags.

    <CsoundSynthesizer>
    <CsOptions>
    ; Select audio/midi flags here according to platform
    -odac -d   ;;;realtime audio out
    ;-iadc    ;;;uncomment -iadc if realtime audio input is needed too
    </CsOptions>
    <CsInstruments>

    ; By Stefano Cucchi - 2020

    ; Initialize the global variables.
    sr = 44100
    ksmps = 32
    nchnls = 2
    0dbfs = 1

    instr 1

    ; Instr 1 trigger instr 2: every time you press any key, instr 2 produce a note.
    ; The pitch of the tone produced by instr 2 is equal to the ASCII code of the key pressed.

    gkNumber, gkPress sensekey

    if changed(gkPress) == 1 then
          if (gkPress == 1) then
              event "i", 2, 0, 0.3
          endif
    endif
    endin

    instr 2

    iCps init i(gkNumber)
    print iCps
    asig oscili 0.2, iCps, 1
    kenvelope linseg 0, 0.1, 1, 0.1, 1, 0.1, 0
    outs asig*kenvelope, asig*kenvelope
    endin

    </CsInstruments>
    <CsScore>
    f 1 0 4096 10 1 1 0.3 0 0.2 0.3 0.5

    i 1 0 2000
    e
    </CsScore>
    </CsoundSynthesizer>

## See also

Sensing and Control: Keyboard and mouse sensing

## Credits

Author: John ffitch
University of Bath, Codemist. Ltd.
Bath, UK
October 2000

New in Csound version 4.09. Renamed in Csound version 4.10.

Prev | Up | Next
sense | Home | serialBegin