53
\$\begingroup\$

Write a program that creates a two-state checkbox that people can interact with using a mouse.

Specifically, your program should achieve all of the following:

  1. Have a 8×8 pixel (or larger if desired) region of the screen that is the clickable area of the checkbox. Henceforth this region is simply called the checkbox.

  2. When the mouse cursor is moved within the checkbox and the default* mouse button is pressed, the checkbox should toggle states.
    Checked becomes unchecked. Unchecked becomes checked.

  3. The checkbox should not move when it is toggled.

  4. In the checked state, the checkbox may be any colors.

  5. In the unchecked state, the checkbox may be any colors as long as at least 16 pixels are visually distinct from the checked state.

  6. In a single program instance, all checked states should be visually identical to one another and all unchecked states should be visually identical to one another.

  7. Don't end the program until it's explicitly terminated (e.g. via exit button or Alt+F4), so a user can click the checkbox as much as they want.

*You may assume the default mouse button is always left click, but it's also fine to use the default as defined by mouse software or the OS, which may not actually be left click depending on the user.

Notes

  • It does not matter what is outside of your checkbox region. It could just be the desktop. It could be a portion of a console that changes on every toggle.

  • It does not matter what state your checkbox starts in.

  • The checkbox region may have any dimensions at or above 8×8 pixels. It need not be square.

  • You may make minor allowances for settings that are out of your control such as console font size, browser zoom, monitor resolution, etc. As long your program works in a reasonable test setting it should be valid.

  • If your program opens a window, you may assume it has been dragged to an appropriate location on the screen if necessary (e.g. top left corner).

  • You may use markup languages such as HTML or other languages we usually don't consider as full-fledged programming languages.

  • Your checkbox must be be toggleable using the default mouse button. It's alright if it also toggles for other forms of input (e.g. right mouse button), with the exception of mouse motion. i.e. the state should not change if the mouse is merely moved within the checkbox.

  • Screenshots of your checkbox in action are highly encouraged!

Scoring

The shortest code in bytes wins.

Example

A canonical HTML example in 23 bytes.

<input type="checkbox">

For me in Google Chrome, this makes a 12×12 pixel checkbox and about 30 pixels clearly change between checked and unchecked states.

I make the allowance that browser zoom is at 100%.

\$\endgroup\$
14
  • 2
    \$\begingroup\$ Can the checkbox cover the entire screen? \$\endgroup\$
    – user41805
    Feb 5, 2017 at 11:19
  • \$\begingroup\$ @KritixiLithos Yes, that would be valid.Though note that you may, if desired, count a particular 8x8 region as the official checkbox, even if a larger region works. \$\endgroup\$ Feb 5, 2017 at 11:22
  • \$\begingroup\$ @Helka Homba - is it allowed for an app to display other controls, aside the checkbox itself ? \$\endgroup\$
    – zeppelin
    Feb 5, 2017 at 12:54
  • \$\begingroup\$ Is it OK for the checkbox to toggle on other sorts of input? (For example, is a solution correct if the checkbox toggles on any sort of user input, regardless of whether it's a mouse click, a mouse movement, or a keypress? It'd toggle on a mouse click, but it'd toggle on other stimuli too.) \$\endgroup\$
    – user62131
    Feb 5, 2017 at 16:27
  • 1
    \$\begingroup\$ @zeppelin "It does not matter what is outside of your checkbox region. It could just be the desktop. It could be a portion of a console that changes on every toggle." So, as long as the checkbox is there, it doesn't matter what else is. \$\endgroup\$ Feb 5, 2017 at 20:42

58 Answers 58

33
\$\begingroup\$

Mathematica, 10 bytes

Checkbox[]

Assumes Mathematica's notebook environment.

The two states look like this:

enter image description here

\$\endgroup\$
3
  • 76
    \$\begingroup\$ Sure. Mathematica cheats again. \$\endgroup\$ Feb 5, 2017 at 12:45
  • 2
    \$\begingroup\$ It's funny how the comment has more upvotes than the answer \$\endgroup\$
    – user60199
    Feb 9, 2017 at 15:56
  • 5
    \$\begingroup\$ @RudolfL.Jelínek it's funny how the answer has any upvotes at all... \$\endgroup\$ Feb 9, 2017 at 18:28
32
\$\begingroup\$

HTML, 20 bytes

Don't know how valid this will be as an answer. HTML requires the tag to be closed to legally validate, but modern browsers will automatically close them so it's executable.

<input type=checkbox

\$\endgroup\$
4
  • 3
    \$\begingroup\$ I was looking at this as an option earlier (without quotes) but completely missed the closing >. +1 and valid IMHO. \$\endgroup\$
    – ElPedro
    Feb 5, 2017 at 21:08
  • 1
    \$\begingroup\$ On this site, languages are defined by their implementation, so this seems fine to me. \$\endgroup\$ Feb 6, 2017 at 15:59
  • 1
    \$\begingroup\$ Thanks @JohnGowers , I've been wondering about the definition of how much abuse is acceptable. \$\endgroup\$
    – Jan
    Feb 6, 2017 at 16:05
  • \$\begingroup\$ I think you need to be more specific about which "modern browsers". It doesn't work in FF for me. \$\endgroup\$ Apr 30, 2020 at 0:08
29
\$\begingroup\$

Bash, 67 55 50 35 32 31 bytes

:()(read -p$[i^=1]
›?9h -sn6
:)

The code contains a CR byte (first newline) and a CSI byte (small right angle bracket with the Windows-1252 encoding). It requires xterm (or equivalent) and the ISO-8859-1 encoding (or similar).

The code defines a function named :, which alternately displays a white 1 or 0 in a black rectangle. There are exactly 24 differing pixels with xterm's default settings. (proof)

For an older version that toggles the color of the entire terminal, check revision 10 of this answer.

Thanks to @ais523 for suggesting xterm, which saves 4 bytes!

How it works

:(...) creates a function named : that executes ....

The read command does the actual magic.

  • -p specifies an input prompt, which is printed to STDOUT.

    • $[i^=1] is an arithmetic expansion which XORs the variable i with 1. i may initially be unset; when this occurs, it will be treated as 0.

    • The carriage return places the cursor back at the beginning of the line.

    • ›?9h captures the mouse in supported terminals. Each click will send six characters to the terminal, namely ←[Mbxy, where represents the ESC byte (0x1b). ←[ indicates an escape sequence, M the mouse, b the button (0x20 + the button number), x the x coordinate of the click (0x20 + coordinate), and y the y coordinate.

    • -sn6 makes read silent (the mouse input won't be echoed to STDERR) and stops after reading exactly 6 bytes. It saves the input in the REPLY variable, but we're not interested in the output it produces.

Finally, once read finishes (after exactly one mouse click), : recursively calls itself, entering an infinite loop.

Creation and invocation

$ echo $':()(read -p$[i^=1]\r\x9b?9h -sn6\n:);:' > checkbox.sh
$ xxd -c 17 -g 1 checkbox.sh
0000000: 3a 28 29 28 72 65 61 64 20 2d 70 24 5b 69 5e 3d 31  :()(read -p$[i^=1
0000011: 5d 0d 9b 3f 39 68 20 2d 73 6e 36 0a 3a 29 3b 3a 0a  ]..?9h -sn6.:);:.
$ LANG=en_US xterm -e bash checkbox.sh

Output

screencast

\$\endgroup\$
7
  • \$\begingroup\$ I was working on a similar solution to this myself (although in a different language, forcing me to give up because I couldn't find a way to take input without a newline). A likely improvement: change 1000h to 9h (the difference is that 1000h handles both mouse press and release, and 9h handles only mouse release). This might or might not need changes elsewhere in the code. \$\endgroup\$
    – user62131
    Feb 5, 2017 at 21:26
  • \$\begingroup\$ @ais523 I tried that, but at least my terminal emulator (Konsole) doesn't seem to support it. Do you know one that does? \$\endgroup\$
    – Dennis
    Feb 5, 2017 at 21:28
  • \$\begingroup\$ gnome-terminal seems to work, that's what I was using for testing (edit: but I don't think it supports a 1-byte CSI). xterm should also work and might be easier to install if you have a KDE system. (I just tested this myself; it works with \x1b[ but not with \x9b for some reason, possibly related to my stty settings rather than to the terminal itself.) \$\endgroup\$
    – user62131
    Feb 5, 2017 at 21:29
  • \$\begingroup\$ @ais523 I do have xterm. I'll check when I'm back at my PC. \$\endgroup\$
    – Dennis
    Feb 5, 2017 at 21:30
  • 9
    \$\begingroup\$ I love how it becomes :) from :( by the end \$\endgroup\$ Feb 6, 2017 at 23:08
25
\$\begingroup\$

GitHub Flavored Markdown - 6 bytes

- [ ] 

(There is a trailing space)

In a gist : check/not check

\$\endgroup\$
4
  • \$\begingroup\$ Sure this matches the 2nd half of the 1st sentence, “Write a program that creates a two-state checkbox that people can interact with using a mouse.”? \$\endgroup\$
    – manatwork
    Feb 7, 2017 at 19:58
  • 1
    \$\begingroup\$ Yes, if you paste it in a gist and create the gist (for "compilation") you will be able to interact with it. \$\endgroup\$
    – Axel
    Feb 7, 2017 at 20:18
  • \$\begingroup\$ Interesting. Thanks @Ax31. I never used gists. \$\endgroup\$
    – manatwork
    Feb 7, 2017 at 20:36
  • 2
    \$\begingroup\$ I would not have expected "Github Flavoured Markdown". \$\endgroup\$ Feb 8, 2017 at 20:41
24
\$\begingroup\$

HTML + JavaScript, 32 30 25 chars

<p onclick=innerHTML^=1>0

\$\endgroup\$
3
  • 3
    \$\begingroup\$ this is implicit and can be removed. Also, innerHTML is shorter. \$\endgroup\$
    – Dennis
    Feb 6, 2017 at 17:41
  • \$\begingroup\$ @Dennis, strange with this... Thank you. \$\endgroup\$
    – Qwertiy
    Feb 6, 2017 at 17:59
  • 1
    \$\begingroup\$ +1 for not using the <input> element \$\endgroup\$
    – RedClover
    Nov 3, 2017 at 15:38
22
\$\begingroup\$

Processing, 78 66 65 63 61 bytes

2 bytes saved thanks to @TuukkaX by using -a%2 instead of a%2*-1

2 bytes saved thanks to @TuukkaX by using a=-a

int a=1;void draw(){background(a);}void mousePressed(){a=-a;}

The checkbox alternates between being black and white.

Explanation

int a=1;                   //the variable that contains the state of the checkbox
void mousePressed(){a=-a;} //if the mouse is pressed, change the sign of a

Now there are many other alternatives like using an if-statement to process this, but then it gets weird and constantly changes the background while the mouse is being pressed. I first tried mouseButton, but then I would need more conditions and more code that will just end up much more verbose.

Now for mousePressed() to be called forever (otherwise it would just stop after the program has just started), we need a draw() function.

void draw(){               //loop infinitely
  background(a);           //set the sketch's background to -a%2
  //if a ==  1, the background colour is set to almost black
  //if a == -1, the background colour is set to white
}

enter image description here


A smaller checkbox would be 90 bytes (the checkbox is in the top left corner, so we can remove some mouse conditions):

int a=1;void draw(){fill(a);rect(0,0,8,8);}void mousePressed(){if(mouseX<9&mouseY<9)a=-1;}
\$\endgroup\$
4
  • 2
    \$\begingroup\$ Clever use of how Processing accept single integers as grayscale colours! \$\endgroup\$
    – FlipTack
    Feb 5, 2017 at 11:58
  • \$\begingroup\$ @FlipTack It seems that white can also be expressed as fill(-1) \$\endgroup\$
    – user41805
    Feb 5, 2017 at 12:25
  • 1
    \$\begingroup\$ I have an idea. Initialize a to 1 instead of 0. Change a++ to a=-a, which makes -a%2 just a. This will toggle a between 1 and -1. I don't know what color 1 produces though. \$\endgroup\$
    – Yytsi
    Feb 5, 2017 at 13:40
  • \$\begingroup\$ @TuukkaX Yup it works. color(1) produces #010101 which is very close to black \$\endgroup\$
    – user41805
    Feb 5, 2017 at 13:54
22
\$\begingroup\$

Unix Shell (+x11-apps), 3 bytes

Disclaimer

The answer below is boring, borderline and "feels cheating" (at least to me).

Yet, it does not seem to violate any of the challenge rules "as written", or "default loopholes", so I'm posting it.

(If you can point a specific rule it breaks, please comment on !)

Golfed

xgc

The xgc program demonstrates various features of the X graphics primitives.

The xgc screen is actually filled with various checkboxes:

enter image description here

and "sticky buttons" (which qualify as checkboxes, under this challenge rules):

enter image description here

\$\endgroup\$
0
18
\$\begingroup\$

AHK, 25 bytes

Gui,Add,Checkbox
Gui,Show

Output -

Mouse pointer is not captured in this gif

\$\endgroup\$
17
\$\begingroup\$

Scratch, 2 blocks

enter image description here

Put this script in the default sprite (which comes with 2 costumes)

\$\endgroup\$
1
14
\$\begingroup\$

Python REPL, 45 bytes

from tkinter import*
Checkbutton(Tk()).pack()
\$\endgroup\$
3
  • \$\begingroup\$ I don't participate in CG nor do I often read these posts. However it really surprises me that you are allowed to use external modules. Isn't that cheating? I don't know how many bytes the Tk source code is, but really shouldn't these bytes be added to your byte count? \$\endgroup\$
    – FMaz
    Feb 11, 2017 at 9:09
  • 1
    \$\begingroup\$ @Krazor tkinter is a standard library for Python (but even if it wasn't I'd only have to declare Python REPL + tkinter). This means that I can use it's features by simply importing it. The penalty you suggest is similar to asking users to include in the byte count the number of bytes in the source of the language you're writing in. Here we allow any language or module to compete as long as it was written and published publicly before the challenge was posted. \$\endgroup\$
    – Blue
    Feb 11, 2017 at 9:27
  • \$\begingroup\$ Okay, thank you for the clarification. \$\endgroup\$
    – FMaz
    Feb 11, 2017 at 9:41
14
\$\begingroup\$

GeoGebra, 10 bytes

Checkbox[]

Entered into the input bar.

Here is a gif of the execution:

Program execution

\$\endgroup\$
2
  • 7
    \$\begingroup\$ Whoah, GeoGebra. I remember that. It's been a while. \$\endgroup\$
    – anon
    Feb 5, 2017 at 21:18
  • \$\begingroup\$ I know this is off-topic but do you go to Waterloo Collegiate Institute? Just out of curiosity, based on your name. I'm not a stalker don't worry :) \$\endgroup\$
    – hyper-neutrino
    May 23, 2017 at 23:34
13
\$\begingroup\$

Tcl/Tk, 21 byte

Golfed

grid [checkbutton .c]

enter image description here

\$\endgroup\$
11
\$\begingroup\$

HTML with JavaScript - 136 124 120 115 113 111 109 79 bytes

<p style=height:20;border:solid onClick=t=this;t.innerHTML=(t.e=!t.e)?'x':''>

-2 with thanks to @KritixiLithos

Reduced to 79 after some fantastic golfing from @Jan. Although the new version fits the requirements I have kept the old version for professional pride reasons.

While it is perfectly valid to use built-ins, I thought it was more fun to create my own from scratch. Tested with Chrome and Firefox. border:solid is there for IE compatibility but I don't know if it is actually needed in newer IE versions - I don't have IE available to test.

The px in the height is there for the benefit of "Run code snippet" so is not included in the "real" byte count. It works fine in the browser without it.

<p style=height:20px;border:solid onClick=t=this;t.innerHTML=(t.e=!t.e)?'x':''>

Previous version:

<p id=d style="width:20px;height:20px;border:solid 1px"onClick="d.innerHTML=d.innerHTML=='x'?'':'x'"align=center>

\$\endgroup\$
18
  • 1
    \$\begingroup\$ Can you do id=d to remove d=this? \$\endgroup\$
    – user41805
    Feb 5, 2017 at 14:15
  • 1
    \$\begingroup\$ @KritixiLithos - Indeed I can. Thanks and well spotted :) \$\endgroup\$
    – ElPedro
    Feb 5, 2017 at 14:23
  • 1
    \$\begingroup\$ You don't need to set the width, the rules say it can be any dimensions. If you store the state on a property on this you'll save setting the id. If you store this in a property you'll save a few bytes. The align is unnecessary. Since there are no spaces in the attribute strings, you don't need the quotes. <p style=height:20px;border:solid onClick=t=this;t.innerHTML=(t.e=!t.e)?'x':''> 79 bytes \$\endgroup\$
    – Jan
    Feb 5, 2017 at 19:19
  • \$\begingroup\$ @Jan - Thanks! The width bit is my own professional pride. If I don't set it it looks ugly and since this answer is never going to win and was just posted as an alternative I'll probably leave it there. Other than that some great hints. Many thanks. Will update tomorrow. \$\endgroup\$
    – ElPedro
    Feb 5, 2017 at 20:26
  • \$\begingroup\$ Thanks @Easterly. Updated the code but forgot the other bits. It's getting late. \$\endgroup\$
    – ElPedro
    Feb 5, 2017 at 20:44
10
\$\begingroup\$

Minecraft 1.0.0, 1 byte

A lever in Minecraft meets all the criteria for a check box. This answer is only valid for older versions of the game where punching a lever still toggles it.

enter image description here

Scored using @Connor O'Brien's suggested byte count.

\$\endgroup\$
5
  • \$\begingroup\$ A difference of 3 between two answers hardly makes or breaks a consensus, but considering that Conor's answer is quite controversial (currently at +24/-10) and that the accepted answer is overwhelmingly well received (+17/-0), I think the latter should be used to score Minecraft answers. \$\endgroup\$
    – Dennis
    Feb 7, 2017 at 3:02
  • \$\begingroup\$ However there are no structure blocks in Minecraft 1.0.0 \$\endgroup\$
    – user46167
    Feb 8, 2017 at 12:37
  • 1
    \$\begingroup\$ It's one n in Conor, btw :) \$\endgroup\$ Mar 28, 2017 at 17:38
  • \$\begingroup\$ Or you could rebind the left and right mouse buttons in any newer version to make it valid ;D \$\endgroup\$ Jun 30, 2017 at 12:27
  • 1
    \$\begingroup\$ @ConorO'Brien Isn't that 1 block? Why 1 byte? I don't think mc stores every block as one byte... \$\endgroup\$
    – RedClover
    Nov 3, 2017 at 15:45
10
\$\begingroup\$

Any version of Windows command line, 3 bytes

(cmd.exe, batch, powershell, start->run, whatever)

osk

Opens the on-screen-keyboard which has several "checkboxes" such as shift, ctrl, alt, capslock keys Similar to the xgc answer, and inspired by the SmileBASIC answer screenshot.

\$\endgroup\$
8
\$\begingroup\$

TI-Basic, 15 bytes

While 1:Shade(0,9:Input :ClrDraw:Input :End

Fairly straightforward. The checkbox covers approximately 40% of the screen and alternates between black and white. When checked, the screen will look something like this:

screen

\$\endgroup\$
7
\$\begingroup\$

Octave, 36 20 bytes

uicontrol('sty','c')

Thanks to @LuisMendo saved 16 bytes!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ uicontrol('sty','c') also works \$\endgroup\$
    – Luis Mendo
    Feb 5, 2017 at 14:03
  • \$\begingroup\$ @LuisMendo Thanks! 1/2 byte counts saved! \$\endgroup\$
    – rahnema1
    Feb 5, 2017 at 15:07
7
\$\begingroup\$

KV Lang, 8 bytes

CheckBox

enter image description here


assuming a really basic Python environment for kv lang to even run:

from kivy.app import runTouchApp
from kivy.lang import Builder
runTouchApp(Builder.load_string('CheckBox'))

This works basically because CheckBox is a Kivy widget included in a kivy.Factory as each widget from the standard library.

That simple string creates an instance of CheckBox and because of runTouchApp expecting a widget to run I can use Builder.load_string.

The background of load_string handles basically every kv string and if there is a single instance of a widget defined (e.g. CheckBox), then it is elevated to a root widget position, for example:

Application
    openGL Window
        root widget(mostly layout)
            main tree
            and some widgets
            ...
\$\endgroup\$
7
\$\begingroup\$

SmileBASIC, 49 bytes

@L
O=T
TOUCH OUT T,,
X=T<O!=X
KEY 1,""[X]GOTO@L

and are an empty box and a checked box in SB's font.
Toggle the checkbox by touching the screen (the closest thing to clicking with a mouse)

enter image description here enter image description here

Cheating answers:

0 bytes:

(the on-screen keyboard has buttons that act like checkboxes, such as the caps lock and insert keys)

14 bytes:

EXEC"SYS/SBGED

Opens up the built in graphics editor, which has multiple checkbox-like buttons

\$\endgroup\$
2
  • \$\begingroup\$ This is just brilliant, though I don't really understand what the 4th line is doing. Could you explain? \$\endgroup\$
    – snail_
    Feb 8, 2017 at 1:06
  • \$\begingroup\$ T<O is true when you stop touching the screen, and != is used as a logical XOR. X=X XOR (O && !T) \$\endgroup\$
    – 12Me21
    Feb 8, 2017 at 1:31
7
\$\begingroup\$

C#, 124 120 bytes

saved 4 bytes thanks to grabthefish.

checkbox checked and not checked note: because of edits, the picture doesn't exactly represent what the code makes, the "checkBox1" isn't displayed with the current code.

using System.Windows.Forms;class P{static void Main(){var a=new Form();a.Controls.Add(new CheckBox());a.ShowDialog();}}

ungolfed: using System.Windows.Forms; class P{ static void Main(){ var a=new Form(); a.Controls.Add(new CheckBox()); Application.Run(a); a.ShowDialog(); } }

\$\endgroup\$
7
  • \$\begingroup\$ If you didn't find anything about this on meta, ask about this on meta. \$\endgroup\$ Feb 6, 2017 at 8:58
  • \$\begingroup\$ You can get rid of the spaces in var a = new Form();. \$\endgroup\$
    – clismique
    Feb 6, 2017 at 9:39
  • \$\begingroup\$ @Qwerp-Derp I removed them for counting, I just pasted the wrong thing, thanks. \$\endgroup\$
    – satibel
    Feb 6, 2017 at 9:57
  • \$\begingroup\$ you could use a.ShowDialog(); to save a few bytes \$\endgroup\$ Feb 9, 2017 at 12:44
  • \$\begingroup\$ thanks @grabthefish, though c# still loses to java :( \$\endgroup\$
    – satibel
    Feb 9, 2017 at 13:09
7
\$\begingroup\$

Excel VBA, 38 32 31 29 Bytes

Immediates Window function that inserts a checkbox of height=9 and width=9, at the cell A1 on the first sheet of the active Excel VBA project

Sheet1.CheckBoxes.Add 0,0,9,9

-6 Thanks to pajonk for replacing [A1],[A1],9,9 with 0,0,9,9

-1 for replacing Sheets(1) with [Sheet1]

-2 for realizing the above was stupid and using Sheet1 instead

\$\endgroup\$
4
  • \$\begingroup\$ Seems that 1,1,9,9 as arguments works as well. \$\endgroup\$
    – pajonk
    Feb 5, 2017 at 18:14
  • 1
    \$\begingroup\$ You dont need the Sheet(1)., you can run this quickly enough in Excel but CheckBoxes is inherint to the active sheet \$\endgroup\$
    – Mr.Burns
    Feb 6, 2017 at 10:56
  • \$\begingroup\$ @Mr.Burns I have checked into this, but without directly referencing any Worksheet object, the CheckBoxes.Add method throws an error stating "Object Required" \$\endgroup\$ Feb 6, 2017 at 22:27
  • \$\begingroup\$ @TaylorScott You are correct, I put the code in a sheet module rather than a normal module .. My mistake xD \$\endgroup\$
    – Mr.Burns
    Feb 7, 2017 at 9:36
6
\$\begingroup\$

Ruby with Shoes, 16 characters

Shoes.app{check}

Sample output: checkbox created with Ruby

\$\endgroup\$
0
6
\$\begingroup\$

HTML with JavaScript, 46 33 28 bytes

<body onclick=innerHTML^=1>0

v2

<body onclick=this.innerHTML^=1>0

v1

<body onclick=t=this;t.innerHTML=t.e=!t.e||''>

This is a variant on @ElPedro's answer: Create a Checkbox . Got down to 28 bytes with the help of @Albert Renshaw It could be further reduced, but that would make it identical to Create a Checkbox

The whole application area is clickable. It will display 0 in the off state and 1 in the on state. Try it out at https://jsbin.com/nuhetiyocu/1/edit

\$\endgroup\$
10
  • 1
    \$\begingroup\$ Here's a neat variation at 39 bytes. <body onclick=t=this;t.innerHTML^=t?1:0 The 16 pixel rule between state change is up for debate though. \$\endgroup\$ Feb 7, 2017 at 9:28
  • \$\begingroup\$ 38 <body onclick=this.innerHTML^=this?1:0 \$\endgroup\$ Feb 7, 2017 at 9:30
  • 1
    \$\begingroup\$ Oh no with your help I'm down on 33 already :) \$\endgroup\$
    – Jan
    Feb 7, 2017 at 9:32
  • 1
    \$\begingroup\$ 28 <body onclick=innerHTML^=1>0 \$\endgroup\$ Feb 7, 2017 at 9:36
  • 1
    \$\begingroup\$ Now that there's always a value inside the container, it could be changed from the body to a different element. But I just noticed that would make it identical to another answer codegolf.stackexchange.com/questions/109155/create-a-checkbox/… so I'm just leaving it as is \$\endgroup\$
    – Jan
    Feb 7, 2017 at 9:45
6
\$\begingroup\$

Powershell v5+, 94 Bytes

($a=[Windows.Forms.Form]::new()).Controls.Add([Windows.Forms.Checkbox]::new());$a.ShowDialog()

enter image description here enter image description here

This is pretty terrible, but I can't get the statements any shorter with either Add-Type or using namespace - and the variable is required, since I need to reuse the object to show the form, and Controls.Add() returns nothing usable.

ShowDialog is required on my system to have the box be interactive after running this command, otherwise the form is frozen.

also System.Windows.Forms.Form can only be shortened to Windows.Forms.Form before (at least my own) system stops auto completing it in a fresh session.

\$\endgroup\$
2
  • \$\begingroup\$ Nice! Might want to add that this requires version 5+. \$\endgroup\$
    – briantist
    Feb 7, 2017 at 0:56
  • \$\begingroup\$ @briantist updated, thanks. \$\endgroup\$
    – colsw
    Feb 7, 2017 at 2:25
5
\$\begingroup\$

Operation Flashpoint scripting language, 273 bytes

To show the checkbox:

createDialog"b"

But first it must be defined:

Save the following to a file named description.ext in the mission folder:

class b{idd=999;movingEnable=1;class controls{class c{idc=457;x=0.1;y=0.1;w=0.1;h=0.1;text="";action="if(ctrlText 457=={})then{ctrlSetText[457,{V}]}else{ctrlSetText[457,{}]}";type=1;style=2;colorText[]={1,1,1,1};font="tahomaB36";sizeEx=0.1;}}}

The checkbox in action:

\$\endgroup\$
4
\$\begingroup\$

VBA (Word), 74 57 bytes

-17 thanks to Taylor Scott

Sub a()
f = a.FormFields.Add(Selection.Range, 71)
End Sub

Filename of the document should be a.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can replace the wdFieldFormCheckBox with 71 (the integer value of the wdFieldFormCheckBox constant) for a savings of 17 bytes \$\endgroup\$ Feb 5, 2017 at 17:27
  • \$\begingroup\$ I think you can remove the newlines for -2 bytes. \$\endgroup\$
    – devRicher
    Feb 5, 2017 at 23:08
4
\$\begingroup\$

Pug, 20 bytes

input(type=checkbox)
\$\endgroup\$
4
  • \$\begingroup\$ Besides which I count is as 23. If you remove the quotes it still works on Firefox and Chrome (can't test on IE) and would come in at 21 in which case I would consider it an improvement on the example and therefore valid. \$\endgroup\$
    – ElPedro
    Feb 5, 2017 at 12:37
  • \$\begingroup\$ @heonlygusti - indeed but without the quotes I would have upvoted :-) btw, the downvote wasn't me. \$\endgroup\$
    – ElPedro
    Feb 5, 2017 at 12:40
  • \$\begingroup\$ I would have up-voted if it wasn't for those quotes, which are not needed. \$\endgroup\$ Feb 6, 2017 at 19:34
  • \$\begingroup\$ @theonlygusti not at all. Give it a try. \$\endgroup\$ Feb 6, 2017 at 20:27
4
\$\begingroup\$

Clojure, 201 195 145 127 bytes

(ns c(:require[quil.core :as q]))(def a(atom nil))(q/defsketch b :draw #(q/background(if@a 0 99)):mouse-clicked #(swap! a not))

-6 bytes by getting rid of the need for m/fun-mode.

-50 bytes by removing the :setup and :size options, since they are defaultable and unnecessary in non-fun-mode. Also removed the unnecessary import for middleware.

-18 bytes thanks to @ASCII-only. Changed to "smaller" colors, and removed an unnecessary space before the dereference operator (@).

Creates a Quil sketch (a Clojure library that provides a layer over Processing), therefore, this requires the Quil library to run.

(ns bits.golf.checkbox
  (:require [quil.core :as q]))

; Mutable global variable to hold the toggle state
(def a (atom nil))

(q/defsketch b
         ; Draw the background color to be either black or red depending on the toggle state.
         :draw #(apply q/background (if @a [0 0 0] [255 0 0]))

         ; Negate the state on mouse click
         :mouse-clicked #(swap! a not))

enter image description here

enter image description here

\$\endgroup\$
7
  • \$\begingroup\$ Can't you replace 255 with 99? Also I'd try removing spaces before/after symbols and seeing if it still works \$\endgroup\$
    – ASCII-only
    Mar 28, 2017 at 12:45
  • \$\begingroup\$ @ASCII-only yes, I could. It made a rant ugly color though iirc, and for some reason I prioritized that at the time. And no, this is as much space as I can remove unfortunately. \$\endgroup\$ Mar 28, 2017 at 13:35
  • \$\begingroup\$ @ASCII-only The problem is symbols can contain almost any character, so really the only "safe neighbours" that you can remove spaces around are any kind of bracket, or quotes. Luckily this is a lisp to the former is pretty prevalent. \$\endgroup\$ Mar 28, 2017 at 13:42
  • \$\begingroup\$ Hmm maybe [0 0 99] then? (some kind of dark blue) \$\endgroup\$
    – ASCII-only
    Mar 28, 2017 at 21:52
  • \$\begingroup\$ @ASCII-only Actually, I think I can drop the 0s now that I think about it. I think Quil auto pads the front with 0s if the vector is to short. If I remember latest I'll fix it up. Thanks for getting me thinking. \$\endgroup\$ Mar 29, 2017 at 13:27
3
\$\begingroup\$

AutoIt, 78 Bytes

GUICreate(0)
GUISetState(GUICtrlCreateCheckbox(0,0,0))
Do
Until GUIGetMsg()=-3

Opens a maximized window that can be closed like any other windows window. The maximization and ... on-top-ness is a side effect of the golfing.

enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ How would I run this on Linux? \$\endgroup\$
    – anna328p
    Feb 7, 2017 at 18:24
  • \$\begingroup\$ @wat wine AutoIt3.exe script.au3, where the script.au3 file contains this code. \$\endgroup\$
    – user42643
    Feb 7, 2017 at 19:13
3
\$\begingroup\$

Python, 62 bytes

from tkinter import*
a=Tk()
Checkbutton(a).pack()
a.mainloop()
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.