Terminal control/Ringing the terminal bell: Difference between revisions
Terminal control/Ringing the terminal bell (view source)
Revision as of 11:07, 1 March 2024
, 2 months agoalarm in BLC
(alarm in BLC) |
|||
(31 intermediate revisions by 17 users not shown) | |||
Line 10:
In most terminals, if the [[wp:Bell character|Bell character]] (ASCII code '''7''', <big><code> \a </code></big> in C) is printed by the program, it will cause the terminal to ring its bell. This is a function of the terminal, and is independent of the programming language of the program, other than the ability to print a particular character to standard out.
<br><br>
=={{header|11l}}==
<syntaxhighlight lang="11l">print("\a")</syntaxhighlight>
=={{header|6800 Assembly}}==
<
.tf bel6800.obj,AP1
.lf bel6800
Line 34 ⟶ 37:
jsr outeee ; and print it
swi ;Return to the monitor
.en</
=={{header|
===Using stdout===
{{trans|X86 Assembly}}
This is how it's ''supposed'' to be done:
<syntaxhighlight lang="asm">.model small
.stack 1024
.data
.code
start: mov ah, 02h ;character output
mov dl, 07h ;bell code
int 21h ;call MS-DOS
mov ax, 4C00h ;exit
int 21h ;return to MS-DOS
end start</syntaxhighlight>
But I couldn't hear anything on DOSBox when doing this.
===The hard way===
This version takes direct control over the PC's beeper to produce a tone whenever <code>BEL</code> is passed to <code>PrintChar</code>.
<syntaxhighlight lang="asm">.model small
.stack 1024
.data
.code
start:
mov al,7
call PrintChar
mov ax,4C00h
int 21h ;return to DOS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PrintChar: ;Print AL to screen
push cx
push bx
push ax
cmp al,7
jne skipBEL
call RingBell
jmp done_PrintChar
skipBEL:
mov bl,15 ;text color will be white
mov ah,0Eh
int 10h ;prints ascii code stored in AL to the screen (this is a slightly different putc syscall)
done_PrintChar:
pop ax
pop bx
pop cx
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
RingBell:
push ax
push cx
push dx
;if BEL is the argument passed to PrintChar, it will call this function and not actually print anything or advance the cursor
;this uses the built-in beeper to simulate a beep
mov al,10110110b ;select counter 2, 16-bit mode
out 43h, al
mov ax,0C00h ;set pitch of beep - this is somewhat high but isn't too annoying. Feel free to adjust this value
out 42h,al
mov al,ah
out 42h,al
mov al,3
out 61h,al ;enable sound and timer mode
mov cx,0FFFFh
mov dx,0Fh ;set up loop counters
beepdelay: ;delay lasts about half a second
loop beepdelay
mov cx,0FFFFh
dec dx
jnz beepdelay
mov al,0 ;mute
out 61h,al ;cut the sound
; mov bl,15
; mov ax,0E20h ;print a spacebar to the terminal
; int 10h ;uncomment these 3 lines if you want the BEL to "take up space" in the output stream
pop dx
pop cx
pop ax
ret
end start</syntaxhighlight>
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC Wait(BYTE frames)
BYTE RTCLOK=$14
frames==+RTCLOK
WHILE frames#RTCLOK DO OD
RETURN
PROC Main()
BYTE
i,n=[3],
CH=$02FC ;Internal hardware value for last key pressed
PrintF("Press any key to hear %B bells...",n)
DO UNTIL CH#$FF OD
CH=$FF
FOR i=1 TO n
DO
Put(253) ;buzzer
Wait(20)
OD
Wait(100)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Ringing_the_terminal_bell.png Screenshot from Atari 8-bit computer]
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Characters.Latin_1;
Line 44 ⟶ 169:
begin
Put(Ada.Characters.Latin_1.BEL);
end Bell;</
=={{header|Applescript}}==
<syntaxhighlight lang
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">print "\a"</syntaxhighlight>
=={{header|Asymptote}}==
<syntaxhighlight lang
See [http://asymptote.sourceforge.net/doc/Data-types.html#index-g_t_0040code_007bbeep_007d-287 beep() in the Asymptote manual].
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">
fileappend, `a, *
</syntaxhighlight>
This requires that you compile the exe in console mode (see Lexikos script to change this) or pipe the file
Line 62 ⟶ 190:
=={{header|AWK}}==
<syntaxhighlight lang="awk">BEGIN {
print "\a" # Ring the bell
}</
=={{header|BASIC}}==
Line 71 ⟶ 198:
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="applesoft
==={{header|Integer BASIC}}===
You can't see it, but the bell character (Control G) is embedded in what looks like an empty string on line 10.
<
20 END</
==={{header|IS-BASIC}}===
<syntaxhighlight lang
==={{header|Locomotive Basic}}===
<syntaxhighlight lang
===
The ZX Spectrum had a speaker, rather than a bell. Here we use middle C as a bell tone, but we could produce a different note by changing the final zero to a different value.
<syntaxhighlight lang
=={{header|Batch File}}==
Source: [http://www.dostips.com/forum/viewtopic.php?f=3&t=5860 Here]
<
for /f %%. in ('forfiles /m "%~nx0" /c "cmd /c echo 0x07"') do set bell=%%.
echo %bell%</
=={{header|BBC BASIC}}==
Assuming that the platform the program is running on rings the bell when CHR$7 is sent to the VDU driver:
<syntaxhighlight lang
=={{header|
<
=={{header|beeswax}}==
<syntaxhighlight lang="beeswax">_7}</syntaxhighlight>
=={{header|Befunge}}==
<syntaxhighlight lang
=={{header|Binary Lambda Calculus}}==
The 2-byte BLC program <code>20 07</code> in hex outputs ASCII code 7.
=={{header|Bracmat}}==
Run Bracmat in interactive mode (start Bracmat without command line arguments) and enter the following after the Bracmat prompt <code>{?}</code>:
<syntaxhighlight lang
Alternatively, run Bracmat non-interactively. In DOS, you write
<pre>bracmat "put$\a"</pre>
Line 124 ⟶ 254:
Assuming the output stream is connected to a TTY, printing BEL should ring its bell.
<
+
+ +
+++
+-+-+
.</
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdio.h>
int main() {
printf("\a");
return 0;
}</
=={{header|C sharp|C#}}==
Inside a function:
<
System.Console.Write("\a"); // will beep
System.Threading.Thread.Sleep(1000); // will wait for 1 second
Line 157 ⟶ 278:
// System.Console.Beep() also accepts (int)hertz and (int)duration in milliseconds:
System.Console.Beep(440, 2000); // default "concert pitch" for 2 seconds
</syntaxhighlight>
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iostream>
int main() {
std::cout << "\a";
return 0;
}</syntaxhighlight>
=={{header|Clojure}}==
<syntaxhighlight lang
=={{header|COBOL}}==
Using the standard screen section:
{{works with|X/Open COBOL}}
{{works with|COBOL 2002}}
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. ring-terminal-bell.
DATA DIVISION.
SCREEN SECTION.
01 ringer BELL.
PROCEDURE DIVISION.
DISPLAY ringer.
STOP RUN.
END PROGRAM ring-terminal-bell.</syntaxhighlight>
Using the ASCII code directly:
{{works with|COBOL-85}}
<syntaxhighlight lang="cobol"> *> Tectonics: cobc -xj ring-terminal-bell.cob --std=cobol85
IDENTIFICATION DIVISION.
PROGRAM-ID. ring-ascii-bell.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
OBJECT-COMPUTER.
PROGRAM COLLATING SEQUENCE IS ASCII.
SPECIAL-NAMES.
ALPHABET ASCII IS STANDARD-1.
PROCEDURE DIVISION.
DISPLAY FUNCTION CHAR(8) WITH NO ADVANCING.
*> COBOL indexes starting from 1.
STOP RUN.
END PROGRAM ring-ascii-bell.</syntaxhighlight>
{{works with|Visual COBOL}}
<
PROGRAM-ID. mf-bell.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 bell-code PIC X USAGE COMP-X VALUE 22.
01 dummy-param PIC X.
PROCEDURE DIVISION.
CALL X"AF" USING bell-code, dummy-param
GOBACK.
END PROGRAM
=={{header|Common Lisp}}==
<
(format t "~C" (code-char 7))
</syntaxhighlight>
=={{header|D}}==
<
import std.stdio;
writeln('\a');
}</
=={{header|dc}}==
<syntaxhighlight lang="dc">7P</syntaxhighlight>
=={{header|Delphi}}==
<
{$APPTYPE CONSOLE}
Line 199 ⟶ 364:
begin
Writeln(#7);
end.</
=={{header|E}}==
<
=={{header|Emacs Lisp}}==
<
(beep) ;; the same thing</
On a tty or in <code>-batch</code> mode this emits a BEL character. In a GUI it does whatever suits the window system. Variables <code>visible-bell</code> and <code>ring-bell-function</code> can control the behaviour.
Line 215 ⟶ 380:
=={{header|F_Sharp|F#}}==
<
Console.Beep()</
=={{header|Factor}}==
<syntaxhighlight lang="factor">USE: io
"\u{7}" print</syntaxhighlight>
Or:
<syntaxhighlight lang="factor">USING: io strings ;
7 1string print</syntaxhighlight>
=={{header|Forth}}==
<syntaxhighlight lang
{{works with|GNU Forth}}
<syntaxhighlight lang="forth">#bell emit</syntaxhighlight>
{{works with|iForth}}
<syntaxhighlight lang="forth">^G emit</syntaxhighlight>
=={{header|FreeBASIC}}==
<
Print !"\a"
Sleep</
=={{header|gnuplot}}==
<
=={{header|Go}}==
<
import "fmt"
Line 246 ⟶ 420:
func main() {
fmt.Print("\a")
}</
=={{header|Groovy}}==
<syntaxhighlight lang
=={{header|Haskell}}==
<
=={{header|Icon}} and {{header|Unicon}}==
Works on both Icon and Unicon.
<syntaxhighlight lang="icon">
procedure main ()
write ("\7") # ASCII 7 rings the bell under Bash
end
</syntaxhighlight>
=={{header|J}}==
This j sentence reads "Seven from alphabet."
<
=={{header|Java}}==
<
public static void main(String[] args){
java.awt.Toolkit.getDefaultToolkit().beep();
Line 277 ⟶ 450:
System.out.println((char)7);
}
}</
=={{header|Joy}}==
<syntaxhighlight lang="joy">7 putch.</syntaxhighlight>
=={{header|Julia}}==
{{works with|Linux}}
<syntaxhighlight lang="julia">
println("This should ring a bell.\a")
</syntaxhighlight>
{{out}}
Line 294 ⟶ 470:
=={{header|Kotlin}}==
{{works with|Windows|10}}
<
fun main(args: Array<String>) {
println("\u0007")
}</
=={{header|Lang}}==
<syntaxhighlight lang="lang">
fn.println(fn.toChar(7))
</syntaxhighlight>
=={{header|Lasso}}==
<syntaxhighlight lang
=={{header|Logo}}==
<syntaxhighlight lang
=={{header|Lua}}==
<
=={{header|M2000 Interpreter}}==
Line 314 ⟶ 495:
===Using Windows Bell===
Async beep. If another start while beeps (it is a bell), then stop
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
After 300 {beep}
Line 325 ⟶ 506:
}
CheckIt
</syntaxhighlight>
Line 336 ⟶ 517:
</pre>
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
After 300 {Tone 200}
Line 347 ⟶ 528:
}
CheckIt
</syntaxhighlight>
Line 358 ⟶ 539:
</pre>
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
After 300 {Tune 300, "C3BC#"}
Line 369 ⟶ 550:
}
CheckIt
</syntaxhighlight>
Line 375 ⟶ 556:
Play a score in each of 16 voices (async, programming internal midi, problem with async in Wine Linux). We can make a piano using keyboard and play/score commands.
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
Score 1, 500, "c@2dc @2ef"
Line 389 ⟶ 570:
}
CheckIt
</syntaxhighlight>
There are other statements like Sound, and Background filename$ to play background music.
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
=={{header|MUMPS}}==
<syntaxhighlight lang="mumps">write $char(7)</syntaxhighlight>
=={{header|Nanoquery}}==
<syntaxhighlight lang="nanoquery">print chr(7)</syntaxhighlight>
=={{header|Nemerle}}==
<
module Beep
Line 410 ⟶ 596:
Beep(2600, 1000); // limited OS support
}
}</
=={{header|NetRexx}}==
<
options replace format comments java crossref symbols binary
Line 434 ⟶ 620:
end
return
</syntaxhighlight>
=={{header|Nim}}==
<
=={{header|Objeck}}==
<
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let () = print_string "\x07"</syntaxhighlight>
=={{header|PARI/GP}}==
{{Works with|PARI/GP|2.7.4 and above}}
<
\\ 8/14/2016 aev
Strchr(7) \\ press <Enter></
;or:
<
{{Output}}
Line 464 ⟶ 653:
=={{header|Perl}}==
<
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\x07"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
Ineffective under pwa/p2js - just displays an unknown character glyph.
=={{header|PHP}}==
<
echo "\007";</
=={{header|PicoLisp}}==
<syntaxhighlight lang
=={{header|PL/I}}==
<
unspec (bell) = '00000111'b;
put edit (bell) (a);</
=={{header|PostScript}}==
The following will only work in a PostScript interpreter that sends output to a terminal. It will very likely not make a printer beep.
<syntaxhighlight lang
=={{header|PowerShell}}==
One can either use the ASCII <code>BEL</code> character which only works in a console (i.e. not in a graphical PowerShell host such as PowerShell ISE):
<
or use the .NET <code>Console</code> class which works independent of the host application:
<syntaxhighlight lang
=={{header|PureBasic}}==
<syntaxhighlight lang
=={{header|Python}}==
In Python 2.7.x:
<
In Python 3.x:
<
=={{header|Quackery}}==
On some platforms the bell will not ring until the output buffer is flushed e.g. by a cr/lf.
<syntaxhighlight lang="quackery">ding</syntaxhighlight>
=={{header|R}}==
<syntaxhighlight lang
=={{header|Racket}}==
<
#lang racket
(require (planet neil/charterm:3:0))
(with-charterm
(void (charterm-bell)))
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>print 7.chr;</syntaxhighlight>
=={{header|Retro}}==
<syntaxhighlight lang
=={{header|REXX}}==
Line 520 ⟶ 719:
However, some REXX interpreters have added a non-standard BIF.
<
/*╔═══════════════════════════════════════════════════════════════╗
║ ║
Line 558 ⟶ 757:
call sound freq, secs /* " " " " 3/10 " */
/*stick a fork in it, we're done making noises.*/</
=={{header|Ring}}==
<
see char(7)
</syntaxhighlight>
=={{header|Ruby}}==
<
=={{header|Rust}}==
<syntaxhighlight lang="rust">fn main() {
print!("\x07");
}</syntaxhighlight>
=={{header|Scala}}==
<
=={{header|Seed7}}==
<
const proc: main is func
begin
write("\a");
end func;</
=={{header|Sidef}}==
<
=={{header|SNUSP}}==
<syntaxhighlight lang
=={{header|Standard ML}}==
<syntaxhighlight lang="sml">val () = print "\a"</syntaxhighlight>
=={{header|Tcl}}==
<
=={{header|UNIX Shell}}==
{{works with|Bourne Shell}} {{works with|bash}}
<
# Ring the terminal bell
# echo "\a" # does not work in some shells
tput bel</
=={{header|Wren}}==
<syntaxhighlight lang="wren">System.print("\a")</syntaxhighlight>
=={{header|X86 Assembly}}==
<
.model tiny
.code
Line 604 ⟶ 815:
int 21h ;call MS-DOS
ret ;return to MS-DOS
end start</
=={{header|XPL0}}==
<
ChOut(0,7)</
=={{header|zkl}}==
<
|