33
5
Output the device's screen resolution in the specific format of [width]x[height]
(without the brackets). For example, an output could be 1440x900
.
Here's an online tester that you can use to check your own screen resolution.
33
5
Output the device's screen resolution in the specific format of [width]x[height]
(without the brackets). For example, an output could be 1440x900
.
Here's an online tester that you can use to check your own screen resolution.
38
(_=screen)=>_.width+"x"+_.height
Outputs as function return
. Add f=
at the beginning and invoke like f()
. Uses parameter-initializing to initialize the parameter _
to screen
object. The rest is self-explanatory.
f=(_=screen)=>_.width+"x"+_.height
console.log(f())
Note: Passing an argument to this function will cause it to fail.
with(screen)alert(width+"x"+height)
Never thought I will one day use with
! I don't think this can be golfed further.
If REPLs are allowed, s=screen,s.width+"x"+s.height
(29 characters) also works. – Kobi – 2017-05-03T12:35:18.697
Oooh. Good use of default argument value. – Matthew Roh – 2017-05-03T12:54:38.867
The 35-byte solution can save five bytes by not bothering to alert
: with(screen)(width+'x'+height)
just returns the appropriate string. – KRyan – 2017-05-04T01:58:49.407
@SIGSEGV Thank you! :) – Arjun – 2017-05-04T04:48:51.037
2This answer is fundamentally flawed. I can cheat it by zooming my browser in and out! – user64742 – 2017-05-04T16:12:02.807
@Arjun I just did. Are you calling me a liar? – user64742 – 2017-05-04T16:24:53.700
@Arjun I'm using the zoom feature in the browser itself and getting anywhere from a few hundred to 4000 in width. Intriguingly the ratio seems to stay the same. – user64742 – 2017-05-04T16:33:20.020
@Arjun your answer isn't giving the resolution of the PC monitor. It's giving the ratio of the browser window based on the current zoom level. At least, that's what Mozilla Firefox is doing. – user64742 – 2017-05-04T16:34:33.620
@TheGreatDuck It must be a bug in Firefox. The specification defines screen.width
as the width of user's screen, and the same goes for screen.height
. (And I was not calling you a liar, I was just saying that there must be a misunderstanding :) ) (I advise you to check the solution in some another browser as this works perfect form me) – Arjun – 2017-05-04T16:43:11.553
@Arjun nope. It seems to be a bug all over the place: https://www.quirksmode.org/blog/archives/2013/11/screenwidth_is.html Here's the chart backing that article https://www.quirksmode.org/mobile/tableViewport.html
– user64742 – 2017-05-04T16:52:55.343@TheGreatDuck That's an old post. New browsers tend to work fine. Try upgrading your browser and then running this. Probably, that will fix the problem! :) – Arjun – 2017-05-04T17:01:38.260
@Arjun my browser is already upgraded to the current version. If you read that post, you'd see that the variable you are describing is being phased out as the role you expect it to be. Newer browsers are trying to make it be the resolution of the window when in full screen mode rather than the pixel count of the monitor viewing it. The point behind that is to make websites scale properly when zooming in and out. – user64742 – 2017-05-04T17:58:23.320
from the article "screen.width should return the dimensions of the ideal viewport. The older implementation returns the number of physical device pixels, but that’s now seen as wrong.". Note: you are relying upon the "older implementation". Also, the charts page has this note: "Last major update on 16 October 2016.". That's only 6 months ago. – user64742 – 2017-05-04T17:59:56.897
Can confirm, zooming messes things up in current versions of Firefox. – AKHolland – 2017-05-05T20:45:26.557
Eyy dudes use chrome firefox sux – Matthew Roh – 2017-05-06T05:24:15.383
1Come on, are you guys even trying: _=screen,_.width+"x"+_.height
, 29 bytes – M28 – 2017-05-07T06:38:02.310
_=>outerWidth+'x'+outerHeight
for 29 bytes, only works in browser JS environments – Bassdrop Cumberwubwubwub – 2017-05-08T11:08:39.400
@BassdropCumberwubwubwub That returns the browser width and height, not the monitor resolution. – Oliver – 2018-01-19T19:44:31.467
33
*sigh* TI-BASIC takes an extra byte for every lowercase letter.
+2 thanks to @Timtech
-3 thanks to @Timtech
:If ΔX>.1
:Then
:Disp "96x64
:Else
:Disp "320x240
This only works because TI-BASIC can only be run on calculators with two different screen resolutions: 96 by 64 and 320 by 240. I just test to see which screen I have by setting the Zoom to something that is different depending on screen resolution then outputting the correct resolution.
I'm marking this as non-competing for now, since it is hard coded.
6Thats a clever abuse ;) – Matthew Roh – 2017-05-02T15:38:21.133
1You can save by not using ZDecimal
and then using a different Xmax
comparison, at least one byte. Also, I think you need to use lowercase x
which is two bytes (x2) instead of the one-byte uppercase equivalent. – Timtech – 2017-05-02T18:22:00.730
@Timtech I have to use a two byte Zoom (like ZDecimal
) because the default zoom (ZStandard
) is the same on both calculators. I'll fix the capitalization, though. – Scott Milner – 2017-05-02T22:47:00.217
1Oh, I see what you mean. If you use ZStandard
though, would ΔX
be different then between the calculators? Also, ZDecimal
is only one byte, so this is 31 bytes. – Timtech – 2017-05-03T10:18:30.080
Ah. You're right. ΔX
is different on the calculators. Thanks! – Scott Milner – 2017-05-03T22:15:40.427
2For some reason, my instant reaction is "that's valid, but wouldn't be valid if there were only one possible screen resolution", but that point of view seems internally inconsistent. So I'm really unsure as to whether this is cheating or not. – None – 2017-05-04T01:08:28.987
You don't have to use the lowercase 'x' since lowercase letters aren't usable out of the box (at least for TI-83/84, which I assume this answer applies to). – Jakob – 2017-08-17T04:04:32.743
@Jakob Lowercase letters are possible on TI 84 CE's, they're just two bytes apiece. OP specified that this challenge is case sensitive. – Scott Milner – 2017-08-17T04:06:57.157
20
_=>(s=screen).width+'x'+s.height
console.log((_=>(s=screen).width+'x'+s.height)())
2the lambda version is acceptable – Felipe Nardi Batista – 2017-05-02T14:35:07.850
6_=>(s=screen).width+'x'+s.height
saves a byte – Felipe Nardi Batista – 2017-05-02T14:38:48.003
@FelipeNardiBatista Thanks, the thought just occurred to me as well :) – SethWhite – 2017-05-02T14:40:44.537
1Good job! +1 :) – Arjun – 2017-05-02T14:42:44.293
5I love how all the JS entries have been consistently shorter than a large number of the other answers. Almost never happens. – Draco18s no longer trusts SE – 2017-05-02T17:29:26.077
10
/*/*/sy*r SPDisplaysDataType|awk '/so/{print$2$3$4}'
Runs system_profiler
, gets the SPDisplaysDataType
information, searches for the first so
in Resolution
, and prints the screen resolution. For multiple screens, this prints all resolutions.
The prior, malcompliant variant:
/*/*/sy*r SPDisplaysDataType|grep so|tr -d 'R :a-w'
I just ran this on my MacBook Pro with a second display attached. I got 2880x1800\n1920x1080@60Hz
(two lines). I don't know if that disqualifies this... or? – Floris – 2017-05-02T16:15:11.697
@Floris did OP specify how to behave when there are multiple screens? – Captain Man – 2017-05-02T16:26:57.723
No - but the format @60Hz
is clearly not in spec. – Floris – 2017-05-02T16:28:39.080
I guess you could tack on a |sed 1q
, bringing the byte count up to 58 bytes. – zgrep – 2017-05-02T20:33:37.990
I fixed the non-compliance by switching to awk
and having one extra byte. :) – zgrep – 2017-05-02T20:52:51.000
9
s=screen;alert(s.width+"x"+s.height)
9
fullScreen();print(width+"x"+height);
fullScreen()
causes the app to launch with the maximum dimensions - the display resolution.
One byte less than the obvious
print(displayWidth+"x"+displayHeight);
8
SysGet,w,0
SysGet,h,1
Send,%w%x%h%
Save this in a file with extension .AHK and run it from a command prompt
1Why not use Send
rather than MsgBox
? – Engineer Toast – 2017-05-02T14:57:02.493
@EngineerToast thanks! That saved two bytes – jmriego – 2017-05-02T15:01:09.787
7
-7 thanks to Martin Ender
-5 (actually 12!) from Leaky Nun , Regex wizardry is beyond me.
This is long but not longer than the horrendous System.Windows.Forms.SystemInformation.PrimaryMonitorSize
solution
(gwmi win32_videocontroller|% v*n)-replace" |x \d+\D+$"
first we Get-WmiObject
(gwmi
) to retrieve the Win32_VideoController
object, which contains a member named VideoModeDescription
, which is a string in the format of 1920 x 1080 x 4294967296 colors
, then I run a regex replace to get correct format.
PS H:\> (gwmi win32_videocontroller|% v*n)-replace" |x \d+\D+$"
1920x1080
I think (gwmi win32_videocontroller|% v*n)-replace" |x[^x]+$"
shaves a couple of bytes by tweaking the regex. – TessellatingHeckler – 2017-05-11T16:49:15.807
7
Thanks to @Johan du Toit for saving a byte!
#import<windows.h>
#define G GetSystemMetrics
f(){printf("%dx%d",G(0),G(1));}
2I was still messing around with 'GetDeviceCaps' until I saw your answer :-) You can still save 1 byte by using the following: #define G GetSystemMetrics f(){printf("%dx%d",G(0),G(1));}
– Johan du Toit – 2017-05-03T18:45:14.133
6
read -aa<<<`xrandr`
echo ${a[7]}x${a[9]::-1}
xrandr
belongs to the X server, on Ubuntu is provided by x11-xserver-utils package.
Sample run:
bash-4.3$ read -aa<<<`xrandr`;echo ${a[7]}x${a[9]::-1}
1920x1080
xrandr|grep -oP '\d+x\d+'|line
Thanks to:
Sample run:
bash-4.3$ xrandr|grep -oP '\d+x\d+'|line
1920x1080
I have no bash with a display, would xrandr|grep *
work? – Jonathan Allan – 2017-05-02T13:13:32.650
Sure. But for now the my grep
and sed
attempts to parse xrandr
's output (https://pastebin.com/uTVcjWCq) were longer.
Maybe xrandr|grep *|cut -d' ' -f1
? (using the matching line from your paste @TIO)
Ah, you mean to pick the resolution from the list by the “*” mark? Thought to that possibility, but I am not sure whether would work with multiple displays connected. As I remember, that would list each connected display's current resolution. – manatwork – 2017-05-02T13:36:00.600
Ah yes it would, not sure what the OP wants in such a scenario though! – Jonathan Allan – 2017-05-02T13:37:26.443
@JonathanAllan No, it wouldn't. An unescaped *
is a glob that the shell expands to all files in the CWD. – Jens – 2017-05-03T11:22:34.317
...update xrandr|grep *
will "work" if the CWD is empty. – Jonathan Allan – 2017-05-03T12:38:31.090
34 chars: xrandr|grep -oP '\d+x\d+(?=.+\*)'
or xrandr|grep -oP '\d+x\d+'|head -1
– markasoftware – 2017-05-05T23:38:13.363
Thank you, @Markasoftware, but actually those are 33 characters. ☺ – manatwork – 2017-05-06T11:06:58.380
And thank you, @manatwork, for letting me learn about line
:) – markasoftware – 2017-05-07T01:56:25.807
6
String f(){java.awt.Dimension s=java.awt.Toolkit.getDefaultToolkit().getScreenSize();return s.width+"x"+s.height;}
This method will not work in a headless installation of Java (like on TIO) because it uses the awt libraries. Under the hood, calling getScreenSize
uses the Java Native Interface to call out (typically into a C library) for the screen width and screen height.
-9 bytes thanks to Olivier Grégoire for reminding me that I can return the string instead of printing it.
2I was just about to post... – Leaky Nun – 2017-05-02T13:24:20.880
@LeakyNun You and me both. +1 Poke. – Kevin Cruijssen – 2017-05-02T13:46:52.717
Too bad the output is restricted to ...x...
, because void f(){System.out.print((java.awt.Toolkit.getDefaultToolkit().getScreenSize()+"").replaceAll("[^\\d,]",""));}
which outputs 1920,1200
is shorter.. – Kevin Cruijssen – 2017-05-02T14:21:21.810
1@KevinCruijssen yeah I did try playing with that as well. The real "too bad" is that using regex in java is so heavy in terms of byte count. – Poke – 2017-05-02T14:56:59.100
1@Poke You're indeed right. I have been able to use that what I show above with an x
instead of ,
by using some regex replacement, but it's five bytes more than your current answer: void f(){System.out.print((java.awt.Toolkit.getDefaultToolkit().getScreenSize()+"").replaceAll("[^\\d,]","").replace(",","x"));}
or void f(){System.out.print((java.awt.Toolkit.getDefaultToolkit().getScreenSize()+"").replaceAll(".*?(\\d+).*?(\\d+).*","$1x$2"));}
Ah well, what isn't heavy in Java.. ;p – Kevin Cruijssen – 2017-05-02T15:02:28.810
If you return a String
instead of printing it, you could reduce it to 114 bytes. Going Java 8? The regex solution is the shortest with 103 bytes. I don't get why you limit yourself to Java 7 and printing on standard output: the challenges accept both Java 8's lambdas and value-returning functions. You should embrace them instead of gimping yourself with older versions. – Olivier Grégoire – 2017-05-03T08:07:50.400
@OlivierGrégoire Good point on returning... Not sure why I went with printing. Also I did forget to include the version! – Poke – 2017-05-03T12:36:11.653
@OlivierGrégoire I'm not a big fan of Java lambdas in golfing because it's not something you can drop into any old class and have it work. It's similar to imports which we as a community agree are required to be explicitly stated. I know there are meta posts on the subject but if I just stick to Java 7 then I can avoid the situation all together. – Poke – 2017-05-03T12:54:00.273
6
_=>{var s=System.Windows.Forms.Screen.PrimaryScreen.Bounds;return s.Width+"x"+s.Height;};
-6 bytes thanks to @TheLethalCoder by reminding me OP didn't mention about printing, so returning a string is also fine. And an additional -6 bytes by changing it to a lambda.
You can save 11 bytes by compiling to a Func<string>
: ()=>{var s=System.Windows.Forms.Screen.PrimaryScreen.Bounds;return s.Width+"x"+s.Height;};
. However, you have a return of void
but you are returning a string
so you need to add 2 bytes for that. – TheLethalCoder – 2017-05-03T09:48:33.180
1The challenge also doesn't state that you can't take input so you could add an unused input to save another byte i.e. _=>{var s=System.Windows.Forms.Screen.PrimaryScreen.Bounds;return s.Width+"x"+s.Height;};
– TheLethalCoder – 2017-05-03T09:49:16.540
1Oh ignore the return comment you're writing the result out, you can save 6 bytes by returning it. – TheLethalCoder – 2017-05-03T09:49:53.427
And unless you can think of a way to get it shorter var s=System.Windows.Forms.Screen.AllScreens[0].Bounds;
would also be the same count but you could golf it with that idea in mind. – TheLethalCoder – 2017-05-03T09:52:50.023
6
SystemInformation[][[1,5,2,1,2,1,2,2,;;,2]]~Infix~x
This may not work for you depending on what devices you have connected (I don't know). This should always work (assuming you have at least one screen hooked up):
Infix[Last/@("FullScreenArea"/.SystemInformation["Devices","ScreenInformation"][[1]]),x]
SystemInformation[]
returns an expression of the form
SystemInformationData[{
"Kernel" -> {__},
"FrontEnd" -> {__},
"Links" -> {__},
"Parallel" -> {__},
"Devices" -> {__},
"Network" -> {__},
}]
We are interested in "Devices"
, which can be accessed directly as SystemInformation["Devices"]
or as SystemInformation[][[1,5,2]]
. The result will be a list of the form
{
"ScreenInformation" -> {__},
"GraphicsDevices" -> {__},
"ControllerDevices" -> {__}
}
We want "ScreenInformation"
, which can be accessed either as SystemInformation["Devices","ScreenInformation"]
or more succinctly as SystemInformation[][[1,5,2,1,2]]
. The result will be of the form
{
{
"ScreenArea" -> {__},
"FullScreenArea" -> {{0,w_},{0,h_}},
"BitDepth" -> _,
"Resolution" -> _
},
___
}
The length of the list will be the number of screens you have connected. The first screen is SystemInformation[][[1,5,2,1,2,1]]
and the width and height can be extracted as SystemInformation[][[1,5,2,1,2,1,2,2,;;,2]]
Then we just insert an Infix
x
for the output format.
5
' '⎕R'x'⍕⌽⊃⎕WG'DevCaps'
⎕WG'DevCaps'
Window Get Device Capabilities
⊃
pick the first property (height, width)
⌽
reverse
⍕
format as text
' '⎕R'x'
Replace spaces with "x"s
"substitute with an "x" at position 5 (the space)" this would cause problems on a small screen, e.g. 640x480 (which VMs use) – Baldrickk – 2017-05-02T13:55:03.793
5
from ctypes import*
u=windll.user32.GetSystemMetrics;
print u(0),'x',u(1)
print u(0),'x',u(1)
is smaller and his example (link) allows it – Felipe Nardi Batista – 2017-05-02T14:20:25.700
1To clarify, If it's equivalent to the output from What is my screen resolution, It's valid.
in that website, there is space between each part – Felipe Nardi Batista – 2017-05-02T14:22:32.000
@FelipeNardiBatista Updated, thanks. – Neil – 2017-05-02T14:24:15.700
4
Ox`ØP(s×Çn)±d+"x"+ight
The compressed string represents with(screen)width+"x"+height
. Ox
evaluates this as JavaScript, and the result is implicitly printed.
4
(-4 chars due to @AppleShell 's help)
Yes. it compiles.
m[3];main(){SDL_Init(32);SDL_GetDesktopDisplayMode(0,m);printf("%dx%d",m[1],m[2]);}
Run with : gcc snippet.c -lSDL2 && ./a.out
3I think you can shorten this by making m
global and omitting int
: m[3];main(){...
– Appleshell – 2017-05-02T15:39:16.683
accessing by m+1
should be shorter than m[1]
right? or isn't that possible in C but only in C++? surely printf has some dereference token – Gizmo – 2017-05-03T09:34:31.940
@gizmo unfortunately AFAIK there is no printf specifier that does such thing .. – dieter – 2017-05-03T11:26:36.363
4
Thanks @Jonathan-allan, @felipe-nardi-batista
from Tkinter import*
print'%sx%s'%Tk().maxsize()
For single display setups, this matches the output from the site. This gives entire resolution for multiple displays.
print'x'....
saves a byte – Felipe Nardi Batista – 2017-05-03T10:52:10.713
v=Tk().maxsize()
, print'%sx%s'%v
saves 9 bytes. – Jonathan Allan – 2017-05-03T13:04:50.907
oops, and then print'%sx%s'%Tk().maxsize()
saves another 4 >_< – Jonathan Allan – 2017-05-03T13:14:17.053
3
xdpyinfo|grep dim|cut -d' ' -f7
From man page:
xdpyinfo - is a utility for displaying information about an X server.
@Floris @manatwork Thanks for saving a few bytes!
Crossed out 4 is still 4 :( – Christopher – 2017-05-02T15:22:17.927
There is no need for spaces around the pipes; I think is safe to search for “dim” only; you can write -d\
instead of -d' '
. Then when it comes to both grep
for a line and cut
a part of that line, usually is shorter with a single awk
call: xdpyinfo|awk '/dim/&&$0=$2'
. – manatwork – 2017-05-02T15:32:05.433
I suspect you can grep something shorter than dimensions
but I don't have xdpyinfo
on my system... – Floris – 2017-05-02T16:27:15.190
3
1This doesn't work. grep *
expands the asterisk to all files in the directory. – Jens – 2017-05-03T08:07:15.003
@Jens Corrected. Thanks for pointing out – Pandya – 2017-05-03T08:56:09.280
Thanks; another hint: the proper spelling for grep|cut
is awk
. – Jens – 2017-05-03T09:11:56.730
It still doesn't work. It outputs *0
. My xrandr output is *0 3360 x 1050 ( 889mm x 278mm ) *0
. – Jens – 2017-05-03T09:15:53.740
@Jens then you need -f2
Btw, Can you check xrandr|awk '/\*/{print $2}'
? – Pandya – 2017-05-03T09:29:57.163
That only outputs 3360
. It works only with xrandr|awk '/*/{print $2 $3 $4}'
. – Jens – 2017-05-03T09:49:09.813
@Jens That's because your xrandr
output 3360x1050
contains white spaces. – Pandya – 2017-05-03T09:50:38.173
3
just for completeness:
PRINT "256x192"
outputs 256x192
. The Spectrum has a fixed hardwired screen resolution.
...and uses a single byte for keywords like PRINT. – Jens – 2019-10-18T08:11:41.590
2
void setup(){fullScreen();print(width+"x"+height);}
This outputs in this format: width height
. Also, the program creates a window that is the size of the screen you are using (because every Processing program creates a window by default) and this program just outputs the height and the width of this window/sketch.
Oh, the format is WIDTHxHEIGHT
. – Matthew Roh – 2017-05-02T15:27:00.530
@SIGSEGV Just noticed it – user41805 – 2017-05-02T15:27:28.903
2
xdpyinfo
+ awk
, 28 bytes$ xdpyinfo|awk /dim/{print\$2}
3360x1050
Tested on Cygwin with dual heads.
1xdpyinfo|awk /dim/{print\$2}
takes 28 bytes not 24 – Pandya – 2017-05-03T09:55:32.727
@Pandya I need new glasses :-) – Jens – 2017-05-03T11:18:50.380
1
puts [winfo screenw .]x[winfo screenh .]
1
I can't golf....
@for /f %%# in ('"@wmic path Win32_VideoController get CurrentHorizontalResolution,CurrentVerticalResolution /format:value"')do @set %%#>nul
@echo %CurrentHorizontalResolution%x%CurrentVerticalResolution%
1
puts `xrandr`.split[7..9].join[0..-2]
Alternate solution (52 bytes):
puts `xrandr`.match(/t (\d+) (x) (\d+),/)[1..3].join
1
((import html-toolkit)
(htmlOnLoad #::((var S(index(getWindow)screen))
(print(+(index S width)"x"(index S height))))))
(Line breaks added for readability)
Finally, my html-toolkit module gets some use! Only works in the Try it Online link, will not work from command line.
A few bytes could be saved if 1024 x 768
could be valid output. We just use (+ .. "x" .. )
to avoid print
's implicit spacing.
Hmm. I tried it online, but it says 2048x1080
for a true 4K screen that's actually 4096x2160
. Any idea why? Firefox 52.0 on FreeBSD 11. – Jens – 2017-05-03T17:25:22.453
No idea. I'm merely grabbing window.screen
and getting the width
and height
attributes from it. I imagine if you opened up the Firefox console and typed in window.screen
you'll see the apparently incorrect 2048x1080
. – Andrakis – 2017-05-04T00:28:41.393
1
f,g=love.window.setFullscreen,love.graphics function love.draw()f(1)w,h=g.getDimensions()f(0>1)g.print(w.."x"..h)end
The programm changes first to fullscreen then it gets the width and height and prints it then :)
1
$ set `xrandr`;echo $6x$8
3360x1050
Tested on a CentOS 5 box with display redirected to a Cygwin machine with two monitors. Here the full xrandr
output is
$ xrandr
SZ: Pixels Physical Refresh
*0 3360 x 1050 ( 889mm x 278mm ) *0
Current rotation - normal
Current reflection - none
Rotations possible - normal
Reflections possible - none
1
-1 thanks to @manatwork
xdotool getdisplaygeometry|tr \ x
There are two spaces after the \
Gets geometry, then uses tr
to replace spaces with x
Is enough to just escape the space, is shorter than quoting it: tr␠\␠␠x
. (Used ␠ to mark spaces here just because HTML rendering collapses consecutive whitespaces.) – manatwork – 2017-05-04T08:31:09.033
1
from PIL.ImageGrab import*;print'%dx%d'%grab().size
Saved 2 bytes by executing in Python 2
Saved 1 byte by changing import
Welcome to the site! I don't have PIL installed, so I can't test, but could you just do i.grab().size
instead of the string formatting? I don't think it has to be exactly in the WxH
format – James – 2017-05-03T21:23:40.120
@DJMcMayhem Hello! I thought the same thing, but OP says specific format of [width]x[height]. i.grab().size
prints out a tuple as (1920, 1080)
. Granite that would save me 8 bytes, I'd rather go for accuracy to OPs requirements – Wondercricket – 2017-05-03T21:26:41.390
1
console.log((k=screen).width+'x'+k.height)
1Hello and welcome to our site! Make sure to include a byte count in your title for [tag:code-golf] questions. – Post Rock Garf Hunter – 2017-05-04T05:25:48.140
Okay, thank you! Sorry, I didn't know it was a requirement. – Will Brickner – 2017-05-05T07:55:24.083
1
system/view/screens/1/size
Outputs for example:
1920x1080
The code is pretty self explanatory. The 1
refers to the first screen
1
@for /f "tokens=1,2delims=x " %%A in ('"wmic path Win32_VideoController get VideoModeDescription|findstr "l""') do @echo %%Ax%%B
Basically the same thing as SteveFest's answer, except I grab a slightly shorter chunk of Wim32_VideoController and then I use something vaguely resembling regex to get the one line that contains the data that I want.
I have no idea why the wmic
string needs to be inside of double quotes to work, and I can't believe that I can't shorten the string at all.
Ordinarily, wmic path Win32_VideoController get VideoModeDescription
will display something like this:
VideoModeDescription
3440 x 1440 x 4294967296 colors
I can use findstr
's super rudimentary attempt at regex to find the letter "l," which only matches the line with the word "colors." From there, that line is delimited on spaces and the letter "x." %%A
contains the first token and %%B
contains the second token. After that, I just display the values.
Aw.. I've been looking for ways to shorten my code... Nice answer anyways – stevefestl – 2017-05-06T04:12:32.167
1
#!racket/gui
(let-values([(x y)(get-display-size #t)])(printf"~ax~a"x y))
Just discovered the (discouraged) shorthand for #lang
. Saves a few bytes! Documentation for get-display-size
.
0
from win32api import*
u=GetSystemMetrics
print u(0),'x',u(1)
0
()=>{var b=System.Windows.Forms.Screen.PrimaryScreen.Bounds;return b.Width+"x"+b.Height;};
This actually is quite a similiar answer to that one, which already exists, however, I'd like to add a few things. This solution acutally needs a reference to System.Windows.Forms and one to System.Drawing. Those references are not normally added for a console application, so I'm not sure wether it's valid. Therefore I wrote a solution without references, however this has 580 287 Bytes:
using System;using System.Runtime.InteropServices;class P{static void Main()=>Console.Write(G().Item1+"x"+G().Item2);static(int,int)G()=>(GetSystemMetrics(0),GetSystemMetrics(1));[DllImport("User32.dll",ExactSpelling=true,CharSet=CharSet.Auto)]static extern int GetSystemMetrics(int n);}
Here's the solution with line breaks:
using System;
using System.Runtime.InteropServices;
class P
{
static void Main() => Console.Write(G().Item1 + "x" + G().Item2);
static (int, int) G() => (GetSystemMetrics(0), GetSystemMetrics(1));
[DllImport("User32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
static extern int GetSystemMetrics(int n);
}
This solution was created with the huge help of AdamSchiavone on Stackoverflow.
I also wrote a C# Interactive programm with 123 Bytes, which automatically adds the references, so it can be executed on every machine without problems and without having the reference problems:
#r "System.Windows.Forms"
#r "System.Drawing"
var b=System.Windows.Forms.Screen.PrimaryScreen.Bounds;b.Width+"x"+b.Height
This basically is the same as the first one, but it adds the reference automatically and as it is executed in the interactive, you don't need the return statement, you can simply leave away the semicolon.
Your missing the ;
on the end of your func. – TheLethalCoder – 2017-05-04T08:23:02.407
Side note: Those references aren't added normally for a Console
application, they are for a Windows Forms
one. Either way they don't need adding into the byte count. – TheLethalCoder – 2017-05-04T08:24:16.693
@TheLethalCoder You're right, edited my answer. – MetaColon – 2017-05-04T12:42:09.070
1In your large solution you can change internal static class Program
to just class P
, you could probably remove static
from the methods and use new P()
, you can change IntPtr
to int
I believe, Move the i()
out of the struct
probably, change the struct
to a class
and probably a couple of other things. – TheLethalCoder – 2017-05-04T12:52:11.093
@TheLethalCoder I'll update my answer. Unfortunately, I won't have time to do this till tomorrow. – MetaColon – 2017-05-04T13:01:50.043
0
_=>{var s=System.Windows.Forms.Screen.PrimaryScreen.Bounds;return s.Width+"x"+s.Height;};
0
This whole answer was written based on http://php-gtk.eu/en/code-hints/grabbing-a-screenshot-with-gdk
<?list($W,$H)=Gdk::get_default_root_window()->get_size();echo$W,x,$H;
Basically, fetches the "root" window (entire screen) and gets it's size.
Another way (based on http://www.kksou.com/php-gtk2/sample-codes/get-the-size-of-display-screen.php):
<?$w=(new GtkWindow())->get_screen();echo$w->get_width(),x,$w->get_height();
Gets the screen where the window was created, displaying it's dimentions.
0
var s=screen;console.log(s.width+'x'+s.height)
I wanted to write it in the comment but I don't have reputation yet. Can someone explain to me why other solutions don't count bytes for console.log()
? If they were counted then this solution would be the shortest.
0
a=get(0,'ScreenS');fprintf('%dx%d',a(3:4))
Uses property shortening. Other than that, this is a nice showcase of the inflexibility of MATLAB indexing, requiring a temporary variable a
. fprintf
prints to stdout
by default.
0
Shameless port of this Java answer
(#(str(.width %1)"x"(.height %1))(.getScreenSize(java.awt.Toolkit/getDefaultToolkit)))
0
0 hw:displaysize? swap . "x" . .
Returns the device's screen resolution of the first display (0) in pixels in the required format.
Output:
1536x864
0
Locally declared windows function and anonymous VBE immediate window function that takes no input and outputs to the VBE Immediate window.
Note: The below may be made into a 64-bit compatible function by the addition of PtrSafe
following Declare
Declare Function GetSystemMetrics Lib"user32.dll"(ByVal i&)As Long
?GetSystemMetrics(0)&"x"&GetSystemMetrics(1)
-1
tr , x</*/*/*/fb0/v*
If the device has frame buffer you can query /sys/class/graphics/fb0/virtual_size
to get resolution.
Width and Height are delimited by ,
so tr translates to x
.
Path to the file is shortened by using * to the point there are no ambiguities.
On my machine that glob also matches 4 files + 1 symlink in /usr/share/games/fortunes/vi, so I have to specify …/0/… instead of …/f/… (not sure whether you can count on that “0”, I'm not framebuffer fan). – manatwork – 2017-05-03T13:22:10.823
That depends upon directories. – Pandya – 2017-05-03T14:04:28.590
several upvoted answers do it as well, edited for a less likely to hapen option – marcosm – 2017-05-03T14:10:20.117
1Note that this gives the size of the console frame buffer, which for X11 is often quite different from the size of the screen you are sitting in front of when logged in over the network. – Jens – 2017-05-03T14:15:24.663
console framebuffer is fbcon, not fb0. This outputs resolution of screen connected as framebuffer device fb0. – marcosm – 2017-05-03T18:45:02.210
1So for all screens or only the primary screen? – MetaColon – 2017-05-02T13:23:04.823
@MetaColon only the primary. I forgot that secondary screens existed. – Matthew Roh – 2017-05-02T13:23:45.293
17The specific output format is no fun, but it's probably too late to change now – Luis Mendo – 2017-05-02T13:29:46.247
3What should the behaviour be if multiple displays are connected? – Jonathan Allan – 2017-05-02T13:40:08.437
4I suppose we're not allowed to first change your resolution and then tell you those values, right? – Engineer Toast – 2017-05-02T15:12:04.203
3APL\360 (can only be run on IBM/360 typewriter environment), 5 bytes:
'0x0'
– Adám – 2017-05-02T16:14:18.500What if you code for a device that only supports one screen resolution? – Neil – 2017-05-02T16:26:22.933
1The edit by @Riker removed the wording To clarify, If it's equivalent to the output from What is my screen resolution, It's valid. That website reports
1920 x 1080
rather than1920x1080
. Can answers separate width, height, and the x by spaces? – Dennis – 2017-05-02T18:24:11.4871Commodore 64 Basic:
PRINT "40x25"
– Mark – 2017-05-04T01:08:31.8774I like that this one disqualifies most golfing languages and encourages people to explore the limits of practical ones. – robbie – 2017-05-04T14:31:45.593
go to http://www.whatismyscreenresolution.com/ . now zoom out a few times (ex if on windows and firefox: ctrl+mouse_wheel_down), and press F5 : you will now "have" a much higher resolution ! ;) I went from 1024x768 to 4267x3413 by this simple easy trick ... I wonder if that site is not tricking me into believing this
– Olivier Dulac – 2017-05-04T16:04:28.580@Mark that's the text size, not resolution in pixels... – Radovan Garabík – 2017-05-05T07:35:06.527
1The resolution checker link from the question gives wrong resolution. 412x732 on my Nexus 5X with 1080x1920... – stevefestl – 2017-05-06T04:25:07.773