MATLAB Answers

How to use keypressfcn to allow the user only to copy from an edittext?

3 views (last 30 days)
theblueeyeswhitedragon
theblueeyeswhitedragon on 15 Apr 2018
function [] = GUI_15()
S.fh = figure('units','pixels',...
'position',[300 300 400 120],...
'menubar','none',...
'name','GUI_15',...
'numbertitle','off',...
'resize','off');
S.ed = uicontrol('style','edit',...
'unit','pix',...
'position',[30 70 340 30],...
'string','This text can be copied but not changed');
S.pb = uicontrol('style','push',...
'unit','pix',...
'position',[30 30 340 30],...
'string','Print to screen');
set([S.ed,S.pb],{'callback'},{{@ed_call,S};{@pb_call,S}}) % Set callbacks.
set(S.ed,'keypressfcn',{@ed_kpfcn,S}) % set keypressfcn.
function [] = pb_call(varargin)
% callback for pushbutton
S = varargin{3}; % Get the structure.
disp(get(S.ed,'string')) % Print to the command line.
function [] = ed_call(varargin)
% Callback for edit
S = varargin{3}; % Get the structure.
set (S.ed,'string','This text can be copied but not changed');
function [] = ed_kpfcn(varargin)
% Keypressfcn for edit
[K,S] = varargin{[2 3]};
if isempty(K.Modifier)
uicontrol(S.pb)
set (S.ed,'string','This text can be copied but not changed');
elseif ~strcmp(K.Key,'c') && ~strcmp(K.Modifier{1},'control')
uicontrol(S.pb)
set (S.ed,'string','This text can be copied but not changed');
end
I have just started learning about GUI in MATLAB and I found this code in LinkExchange, which basically creates an edit-text and push-button and does not allow the user to make modifications to the edit-text but just copy it. The code makes sense till the last function "ed_kpfcn". I don't understand the use of varargin in this case. Also, why for seeing if the 'c' button is pressed we refer to the Key field and for 'control' to the Modifier field?
Also, if you know any other resources for getting started with GUIs in MATLAB, besides the ones provided by Mathworks I would appreciate if you shared them here. Thank you.

  0 Comments

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!