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 702. 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
    ; Audio out   Audio in
    -odac           -iadc    ;;;RT audio I/O
    ; For Non-realtime ouput leave only the line below:
    ; -o sensekey.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>

    ; Initialize the global variables.
    sr = 44100
    kr = 4410
    ksmps = 10
    nchnls = 1

    ; Instrument #1.
    instr 1
      k1 sensekey
      printk2 k1
    endin

    </CsInstruments>
    <CsScore>

    ; Play Instrument #1 for thirty seconds.
    i 1 0 30
    e

    </CsScore>
    </CsoundSynthesizer>

Here is what the output should look like when the "q" button is pressed...

    q i1   113.00000

Here is an example of the sensekey opcode in conjucntion with FLpanel. It uses the file FLpanel-sensekey.csd.

Example 703. Example of the sensekey opcode using keyboard capture from an FLpanel.

    <CsoundSynthesizer>
    <CsOptions>
    ; Select audio/midi flags here according to platform
    ; Audio out   Audio in    No messages
    -odac           -iadc     -d     ;;;RT audio I/O
    ; For Non-realtime ouput leave only the line below:
    ; -o FLpanel-sensekey.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>
    ; Example by Johnathan Murphy

      sr	    =  44100
      ksmps	    =  128
      nchnls    =  2

    	; ikbdcapture flag set to 1
      ikey	    init      1

    	    FLpanel   "sensekey", 740, 340, 100, 250, 2, ikey
      gkasc, giasc	FLbutBank	2, 16, 8, 700, 300, 20, 20, -1
    	    FLpanelEnd
    	    FLrun

        instr 1

      kkey	    sensekey
      kprint    changed   kkey
    	    FLsetVal  kprint, kkey, giasc

        endin

    </CsInstruments>
    <CsScore>
    i1 0 60
    e
    </CsScore>
    </CsoundSynthesizer>

The lit button in the FLpanel window shows the last key pressed.

Here is a more complex example of the sensekey opcode in conjucntion with FLpanel. It uses the file FLpanel-sensekey2.csd.

Example 704. Example of the sensekey opcode using keyboard capture from an FLpanel.

    <CsoundSynthesizer>
    <CsOptions>
    ; Select audio/midi flags here according to platform
    ; Audio out   Audio in    No messages
    -odac         ; -iadc     -d     ;;;RT audio I/O
    ; For Non-realtime ouput leave only the line below:
    ; -o FLpanel-sensekey2.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>
    sr =  48000
    ksmps =  32
    nchnls =  1
    ; Example by Istvan Varga
    ; if the FLTK opcodes are commented out, sensekey will read keyboard
    ; events from stdin
            FLpanel "", 150, 50, 100, 100, 0, 1
            FLlabel 18, 10, 1, 0, 0, 0
            FLgroup "Keyboard Input", 150, 50, 0, 0, 0
            FLgroupEnd
            FLpanelEnd

            FLrun

            instr 1

    ktrig1 init 1
    ktrig2 init 1
    nxtKey1:
    k1, k2 sensekey
            if (k1 != -1 || k2 != 0) then
            printf "Key code = %02X, state = %d\n", ktrig1, k1, k2
    ktrig1 =  3 - ktrig1
            kgoto nxtKey1
            endif
    nxtKey2:
    k3 sensekey
            if (k3 != -1) then
            printf "Character = '%c'\n", ktrig2, k3
    ktrig2 =  3 - ktrig2
            kgoto nxtKey2
            endif

            endin

    </CsInstruments>
    <CsScore>
    i 1 0 3600
    e
    </CsScore>
    </CsoundSynthesizer>

The console output will look something like:

new alloc for instr 1:
Key code = 65, state = 1
Character = 'e'
Key code = 65, state = 0
Key code = 72, state = 1
Character = 'r'
Key code = 72, state = 0
Key code = 61, state = 1
Character = 'a'
Key code = 61, state = 0

## See also

FLpanel, FLkeyIn

## Credits

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

Examples written by Kevin Conder, Johnathan Murphy and Istvan Varga.

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

Prev | Up | Next
sense | Home | serialBegin